-
File I/O -- c++
I have a file that has the following data:
a12 (where a1 is supposed to be a variable called inclass and 2 means the number of records in the file)
andrew1234567abcdefgh
bingoa7654321asdasahd
and a
struct personal {
char name[6];
char number[7];
char instring[8];
};
how should i go about reading the data into the struct using c++ statements?
-
It depends on the data in the file.
If the data is char WITHOUT null-terminated strings
Code:
FILE *fptr;
BOOL ok;
fptr = fopen("myfile.txt")
ok = 0;
while(1)
{
ok = (fgets( personal.name, 6, fptr ) == NULL);
if (ok)
{
break;
}
ok= (fgets(personal.number,7,fptr) == NULL);
ok = (fgets(personal.instring,8,fptr) == NULL)
// assume that all reocrds have all fields
// otherwise you gotta check ok and break
}
-
but what if i got the '\n' to deal with?