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)
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 = newchar[10];
char *T = newchar[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();
}
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.