Cant append a text file using ios::app and ofstream.

0 Vikram Jayanthi · July 31, 2014
When I try to edit a text file using ios::app instead of changing a file it creates a new file with the same name. ios::app is supposed to edit the file not overwrite it. This is just creating a new file with same name.
Code Below:
#include <iostream>
#include<string>
#include <fstream>
using namespace std;
//Original File creation.
int main()
{
string poem="Hello World";
poem.append("\n\t I cant ryhme.");
poem.append("\n\tShit");
ofstream file("poem.txt");
if(!file)
{
    cout<<"ERROR"<<endl;
}
file<<poem<<endl;
cout<<"Done"<<endl;

}

//code to alter the file.
#include <iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
string poemInfo="Bullcrap";
poemInfo.append("\n\n\t Vikram Jayanthi");
ofstream file("poem.txt",ios::app);
if(!file)
{
    cout<<"ERROR"<<endl;
}

file<<poemInfo<<endl;
cout<<"DONE"<<endl;



}
















Post a Reply

Replies

Oldest  Newest  Rating
0 Vikram Jayanthi · August 1, 2014
They are in the same folder.
0 ragmar thomas · August 4, 2014
//code to alter the file.
#include <iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
string poemInfo="Bullcrap";
poemInfo.append("\n\n\t Vikram Jayanthi");
ofstream file;
file.open("poem.txt",ios_base::app);
if(!file)
{
    cout<<"ERROR"<<endl;
}

file<<poemInfo<<endl;
cout<<"DONE"<<endl;
}



make sure as u are told to put them in the same folder or put them in the same file name in your compiler (you can simply copy and paste it in your IDE that is in the same file name)
and you can append it as i appended it
ofstream file;
file.open("poem.txt",ios_base::app);

 and you can have you can have your
Bullcrap

     Vikram Jayanthi
again and again when ever you compile it in your previous
Hello World
     I cant ryhme.
    Shit
  • 1

C++

107,241 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator