I have a question. I wrote and array of 5 structs into a binary file. I need to be able to go to a specific position in a binary file and overwrite that struct with another. I have tried it but when i go to overwrite the struct the ones before it all the data is screwed up.Can anyone help me on how to overwrite a specific person in the file. struct PERSON { int age; int weight; char name[25]; }; void write_names() //WRITES 5 PEOPLE TO THE FILE { ofstream out; PERSON p[5]; int ct; p[0].age = 1; p[0].weight = 1; strcpy(p[0].name, "Num_1"); p[1].age = 2; p[1].weight = 2; strcpy(p[1].name, "Num_2"); p[2].age = 3; p[2].weight = 3; strcpy(p[2].name, "Num_3"); p[3].age = 4; p[3].weight = 4; strcpy(p[3].name, "Num_4"); p[4].age = 5; p[4].weight = 5; strcpy(p[4].name, "Num_5"); out.open("file.dta", ios::binary); for (ct=0; ct<5; ct++) out.write((char*)& p[ct], sizeof(p[ct])); out.close(); } void show_names() //SHOW ALL THE PEOPLE IN THE FILE { ifstream f; f.open("file.dta", ios::binary); PERSON p[10]; int ct; int num = 0; while(!f.eof()) { f.read((char*)& p[num], sizeof(p[num])); num++; } num--; for(ct=0; ct
S
scrname
@scrname
Posts
-
Overwriting struct in binary file -
differencethis is a noob question but i am a noob so i guess it fits me. i want to know what is the difference between all of the catagoreis like it sayes visual c++, managed c++, visual basic, c#, win32 exct... and what is it used for .
-
File I/O Problemya i tryed putting it in a stuct but it would do the same thing so i just tryed it with it out
-
File I/O Problem>>L_Name[x] >>number[x]; x++; } read.close(); //Where the user can enter more name do { cout <<"First Name: "; cin >>F_Name[x]; cout <<"Last Name: "; cin >>L_Name[x]; cout <<"Number: "; cin >>number[x]; x++; cout <<"Would you like to enter another name?(y/n)"; cin >>resp; } while((resp=='y')||(resp=='Y')); y=x; //writes the origianl names then the new name but here is where i get the //set of numbers ofstream write("File.txt"); for(x=0; x -
File I/O Problem>>L_Name[x] >>number[x]; x++; } read.close(); //Where the user can enter more name do { cout <<"First Name: "; cin >>F_Name[x]; cout <<"Last Name: "; cin >>L_Name[x]; cout <<"Number: "; cin >>number[x]; x++; cout <<"Would you like to enter another name?(y/n)"; cin >>resp; } while((resp=='y')||(resp=='Y')); y=x; //writes the origianl names then the new name but here is where i get the //set of numbers ofstream write("File.txt"); for(x=0; x