Load feature is not working...

I'm not getting any error etc.
I have file called "wizytowka.txt" and text in it.
But everything i see is
-
-
0
-
btw - is nothing.
The feature that says "plik nie istnieje" doesn't work too
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
string imie, nazwisko;
int numer;

int main()
{

    fstream plik;
    plik.open("wizytowka.txt",ios::in|ios::app);
    if (plik.good()==false)
    {
        cout <<"Plik nie istnieje!";
        exit(0);
    }
    int nr_lini;
    string linia;

    while(getline(plik, linia))
    {
        nr_lini++;
        switch(nr_lini)
        {
            case 1: imie = linia; break;
            case 2: nazwisko = linia; break;
            case 3: numer=atoi(linia.c_str()); break;
        }



    }

    cout<<imie<<endl;
    cout<<nazwisko<<endl;
    cout<<numer<<endl;


    return 0;
}

What do i do wrong?...
Ok, I know what the problem was, i need to add =1 to "int nr_lini" but, please tell me how does it work? i mean, what's the difference when i add =1 (int nr_lini =1)
Hello glof2,

Adding "= 1" to "int nr_lini" is a fix not an answer. I believe if you run your program you will find that "nr_lini" is one more than you want when starting at one.

The problem started when you defined "int nr_lini" and left it uninitialized. On my compute this usually leaves the variable with a number like "-858993460". You will be adding one to a number like that for a long time before you get to something that is useful.

Always a good idea to initialize your variables when you define them to avoid any problems. The simplest way is int nr_lini{};, since C++11, or as you did int nr_lini = 1;. In a counter variable like "nr_lini" it is usually best to stat at zero.

If you are going to use "strings" in your program it is best to include the header file "<string>".

Hope that helps,

Andy
Hello glof2,

I am not sure what is going on yet, but so far I have figured out that the file is not being read as it should. I have even changed the code around and tried something different and it still does not work. It tells me the file is open, but acts like the file pointer is at the end of the file.

Still working.

Andy
Hello glof2,

Finally figured out the problem I was having. It was not the program, but the input file I used. Some how the input file became corrupted and although it looked like there was something to read it turned out there was nothing in the file. Deleted and remade the input file and everything worked fine.

I would suggest this as a better way of reading the file as it gives you more opportunity to expand the program in the future. Also if there is more than one record in the file this will print all records in the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	while (std::getline(plik, imie))
	{
		std::getline(plik, nazwisko);
		plik >> numer;
		//  Used to clear the new line (\n) from the input buffer.
		plik.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // Requires header file <limits>

		std::cout << imie << std::endl;
		std::cout << nazwisko << std::endl;
		std::cout << numer << '\n' << std::endl;
	}


	plik.close();  // <--- Do not forget to close the file. 


And so that you know the beginning of your program should look like this:

1
2
3
4
#include <iostream>
#include <string>
#include <limits>
#include <fstream> 


Hope this helps,

Andy
I’d also check if there are empty lines in the secret text file “wizytowka.txt” you didn’t share with us.
The following program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// #include <stdlib.h> < -- C header file
#include <cstdlib> // <-- C++ header file
#include <fstream>
#include <iostream>
#include <string> // missing header

std::string imie, nazwisko;
int numer; // <-- global variable: it is initialized to 0 by the compiler

int main()
{
    std::fstream plik("wizytowka.txt", std::ios::in | std::ios::app);
    if(!plik)
    {
        std::cout << "Plik nie istnieje!"; // or can't be opened in general
        exit(0);
    }
    int nr_lini; // <-- local variable: could be set to *any* value!
    nr_lini = 0; //     You need to explicitly initialize it.
    // int nr_lini = 0; <-- you can define and initialize it in a single statement.
    //                      The simplest way is
    //                      int nr_lini {};
    //                      as Handy Andy said.
    std::string linia;

    while(std::getline(plik, linia))
    {
        nr_lini++;  // <-- if you use this on an uninitialized variable, you can
                    //     get any value, even overflow integer capacity.
        switch(nr_lini)
        {
        case 1: imie = linia; break;
        case 2: nazwisko = linia; break;
        case 3: numer = atoi(linia.c_str()); break; // what about std::stoi()?
        }
    }
    std::cout << "imie: " << imie << "\n\nnazwisko: " << nazwisko
              << "\n\nnumer: " << numer << '\n';
    return 0;
}

outputs this:
imie: Lorem ipsum dolor sit amet, ei usu exerci vocibus invidunt, ea tantas civibus sed, ne vitae alterum probatus eam.

nazwisko: Duo vide consul et, ne est abhorreant moderatius.

numer: 666

if “wizytowka.txt” is:

Lorem ipsum dolor sit amet, ei usu exerci vocibus invidunt, ea tantas civibus sed, ne vitae alterum probatus eam.
Duo vide consul et, ne est abhorreant moderatius.
666


and this:
imie: Lorem ipsum dolor sit amet, ei usu exerci vocibus invidunt, ea tantas civibus sed, ne vitae alterum probatus eam.

nazwisko:

numer: 0

if “wizytowka.txt” is:

Lorem ipsum dolor sit amet, ei usu exerci vocibus invidunt, ea tantas civibus sed, ne vitae alterum probatus eam.

Duo vide consul et, ne est abhorreant moderatius.

666

Topic archived. No new replies allowed.