a strange occurance while using fstream

i just give a part of the program i have write:

f.open(mhs.txt, ios::in | ios::out);
int i=0;
char c[11];
do{
f.seekg((MAX_LINE+1) * i++);
f.read(c, strlen(NIM));
c[f.gcount()]='\0';
}while(strcmp(c, NIM)!=0 && strcmp(c, "")!=0);
f.seekg(MAX_LINE * (i-1));

i write this code to search an user id (defined in NIM as const char[11]) in a text file
where each user data recorded in the text file have the same length (defined in MAX_LINE as const int).
if the user id defined in NIM is found, then the program will overwrite the
existing user data with the new one, or if the user id isn't found, then the
program will create a new one at the end of the file.
this is the content of the file:

1100002096 Kevin Octavio Sistem Komputer 5
1100002070 Albert Gunawan Sistem Komputer 5


the value of NIM is 1100002070, so the data i want to be changed is Albert Gunawan,
Sistem Komputer, and 5. but when the program was executed, the result is really strange...
the first user id, 1100002096 changed to 111100002096, where is the '11' come from?
what part of my code is wrong?
i have found the solution. i have to use ios::binary mode:
f.open(mhs.txt, ios::in | ios::out | ios::binary);

but i still don't understand, why it must use ios::binary?
isn't the default mode is text mode? why i have to use binary mode to edit text file??
"but i still don't understand, why it must use ios::binary?"

Because you read a block from a file with the f.read(c, strlen(NIM)); instruction.

http://www.cplusplus.com/reference/iostream/istream/read/

If you want to read in text mode, then you can use the >>operator or getline command (and more things: fgets etc.). And of course it is rewarding to put all data to new line. What I mean:

1100002096 (ENTER)
Kevin Octavio
Sistem Komputer
5
1100002070
Albert Gunawan
...


your program code in this case:

...
string NIM, name, subject, grade;
fstream myfile;
myfile.open("mhs.txt");
myfile >> NIM;
myfile >> name;
...
myfile.close();
@screw

thx for your advice!
Topic archived. No new replies allowed.