append the file

hi
in my code user enter a name and i use the name for create a file with that name,and then i wanna append the file..my code doesn't work when i use fstream::app and the file doesn't change..but when i don't use fstream::app the file changes,but not in append mode!
can anyone help me please?!

string friendname;
cout<<"who do you want to add to your friends?"<<endl;
cin>>friendname;
string friendF=friendname+".txt";
if(existFriend(friendname)){
fstream friendfile(friendF.c_str(),fstream::app);
friendfile<<"request:"<<username<<endl;
cout<<"request sent"<<endl;
return;
}
Show your existFriend() function. It is possible that the control does not reach the statements inside if condition.

Also, please use code tags and indent your code so that it is easier to read:

1
2
3
4
5
6
7
8
9
10
string friendname;
cout<<"who do you want to add to your friends?"<<endl;
cin>>friendname;
string friendF=friendname+".txt";
if (existFriend(friendname)) {
    fstream friendfile(friendF.c_str(),fstream::app);
    friendfile<<"request:"<<username<<endl;
    cout<<"request sent"<<endl;
    return;
} 
Last edited on
i dont know from what it looks like you could just need to add the ios before the app like so

1
2
3
4
5
6
7
8
9
10
string friendname;
cout<<"who do you want to add to your friends?"<<endl;
cin>>friendname;
string friendF=friendname+".txt";
if (existFriend(friendname)) {
    fstream friendfile(friendF.c_str(),fstream, ios::app);
    friendfile<<"request:"<<username<<endl;
    cout<<"request sent"<<endl;
    return;
} 


i know i used to leave it out and sometimes it would help but other times it wouldn't
i used this line:
fstream friendfile(friendF.c_str(),fstream, ios::app);
and i faced compiler error:
expected primary-expression before ‘,’ token
Topic archived. No new replies allowed.