Appending text to a file

Viewed 129

I am trying to append data into a file, and I am having trouble in checking if the input data is already there in the file. If the data is already there in the file, then it won't add that data again. Can anyone tell me what shall I do in this case? I have attached a copy of my code for your reference. Thanks!

void addentry(char* name, char* entry){

ifstream in_file(name);
ofstream out_file(name, ios::app);


out_file << entry << endl;

if(!in_file) {
   cout << "This is the first entry of this person" << endl;
} 


int_file.close();
out_file.close();
}
2 Answers

read file data and count character till EOF (End of file). Check if count is not equal to zero. If it is so append else don't. You have not provided whole code otherwise I would have compiled and then send you code as well.

Related