Doubt about fstream

I'm writing an algorithm that writes in a file 10 characters (name) followed by 9 more characters (telephone), like that:

Jhon 12345678 \n
Albert xxxxxxxx \n

And every time the program starts, i read the file and put these data into a *struct. But, i don't know why this error ocurrs, if i remove the "fio.tellg()" the code doestn work and i only get the first person (In this example, Jhon and 12345678)

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
  void StartFile()
{
    //each record has 19 characters + 1 of \n
    ifstream fio;
    fio.open("cadastro.txt", ios::in);
    if (fio.is_open())
    {        
        //Count gives me the count of characters
        int qt = Count()/20;
        for(int i =0; i<qt; i++)
        {
                char *N = new char[10];
                char *T = new char[9];
                fio.get(N, 10); // 10 Characters

                fio.tellg(); // <--- IF I REMOVE THAT, CODE DOESNT WORKS

                fio.get(T, '\n');

                fio.tellg(); // <--- IF I REMOVE THAT, CODE DOESNT WORKS TOO
                
                AddPeople(N, T);                
        }
    }
    fio.close();
}
Take a look at the reference page for get().
http://www.cplusplus.com/reference/istream/istream/get/

First, I'm not sure that your character arrays N and T are big enough. Remember to allow room for the null terminating character '\0'.

Secondly, the delimiting character '\n' is not removed from the input stream, so after reading the telephone number, there should be another fio.get() to read and discard the newline.

Also, the use of new [] repeatedly in the loop looks like it could cause a memory leak, unless you have a corresponding delete [] somewhere else in the code.
Topic archived. No new replies allowed.