i have a problems with files, i want to read some names(with 30 characters), a '\t' character, and a number (an age), each person separated for a newline character. How i do not know how much "persons" exists in the file, i count the newlines characters but when i open the file, and i read its content in a string and a integer variable, show just a 'B' Oo... I think that problem is with seekg(), the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
ifstream ent("Example.txt");
int c=0;
while (ent) if (ent.get() == '\n') c++;
cout<<c<<endl;
char reg[c][30];
int edad[c];
ent.seekg(0,ios::beg);
ent.read(reg[1],30);//I try with getline
cout<<reg[1]<<endl;//test
/*for (int i=0;ent;i++){
ent.getline(reg[i],30);
ent.ignore();
ent>>edad[i];
ent.ignore();
}
for (int i=0;i<c;i++)
cout<<reg[i]<<'\t'<<edad[i]<<endl;*/
ent.close();
The problem is that i need to use getline() because name[] got ' ' characters... I was testing, look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
ofstream sal("Example.txt");
sal<<"Pedro Perez :"<<18<<endl; //29 characters more the ':'
sal<<"Maria Rodriguez :"<<34<<endl;
sal.close();
ifstream ent("Example.txt");
char name[2][30];
int age;
ent.getline(name[0],30); //here add 29 characters to name[0] more null terminator
ent.ignore(); //here i want to ignore the ':', with or without this line the code don't work
cout<<name[0]; //show "Pedro Perez "
ent>>age; //get 18
cout<<age<<endl; //show 18
ent.close();
The spaces and ':' character are for when i open the file can look it with "format"... Is a test xD for see the behavior of getline and ignore and find the problem on the first code...