c++ question: add line to text file but got a blank first line.

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?
Last edited on
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.
Mmm, yeah, I don't think you can do anything about that unless you create the file to start with or truncate it every time...
thx for helping me...
Topic archived. No new replies allowed.