hi, i have a problem, im starting to use files on c++, and i want to make a program that you give an id, and it first search for it on the file, if it isnt found then you can create a new id and add data to it, the thing is that i think its never saving because the output file is always 0kb
#include<iostream>#include<fstream>usingnamespacestd;structstPeople{intid;charname[15];};intmain(){stPeoplepeople={0,""};fstreamfile;file.open("data.txt",ios::in|ios::app|ios::binary);if(!file){cerr<<"Error, no file"<<endl;return1;}cout<<"Give me the id: ";intnewID;cin>>newID;boolexistID=false;while(!file.eof()){file.read(reinterpret_cast<char*>(&people),sizeof(stPeople));if(people.id==newID){cerr<<"Error, this id is already used"<<endl;existID=true;break;}}if(existID==false){people.id=newID;cout<<"Give me the name: ";fflush(stdin);cin.getline(people.name,15);file.write(reinterpret_cast<char*>(&people),sizeof(stPeople));}file.close();}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hi, i have a problem, im starting to use files on c++, and i want to make a program that you give an id, and it first search for it on the file, if it isnt found then you can create a new id and add data to it, the thing is that i think its never saving because the output file is always 0kb