Here is a part of my code;
the function is used to write things into a txt file.
the function works quite well except there is a blank first line in the txt file.
void addname(string name)
{
getname.open(NAMEFILE,ios_base::app);
if (getname.is_open())
{
getname<<name<<endl;
getname.close();
}
else
{
cout << "ERROR: Could not open the datafile: " << NAMEFILE << endl;
exit(1);
}
}
i think the error is caused by "ios_base::app", which always write to the end of the file.
i want to write to the end of the file, but i don't want a blank 1st line. can anyone help me to solve this problem?
The only thing I can think of is your file already existing and having and having a blank line...delete or rename the file and try again so it creates it for you and see what happens.
the file is existing but it is a blank new file, so there is nothing in it. i try to use ios_base::ate, the word is write to the1st line and there is no blank line there.
but if i use ios_base::ate, when i got more than one line input, only the last line will write into the txt file.