Reputation: 89
I want to add a new line (string) into the end of an existing file. But it didn't work. Here is the code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
ifstream input("Sample.ini");
ofstream output("Sample.ini",ios::app);
cout << "Lines that have existed in file:" << endl;
while (input) // Print out the existed line
{
string newstring;
getline(input,newstring);
cout << newstring << endl;
}
cout << "Line you want to add:" << endl;
string outputstring;
getline(outputstring,output); // get the whole line of outputstring,
// and deliver it into output file
return 0;
}
The first getline which reads lines inside the file to a string works well. But, the second one, is not. The compiler returned like this:
...\file test.cpp|35|error: no matching function for call to 'getline(std::istream&, std::ofstream&)'|
Upvotes: 0
Views: 87
Reputation: 1458
you may want to try poniter
to file, short & simple
#include <stdio.h>
int main ()
{
FILE * pFile;
File = fopen ( "you_file_name.txt" , "a" ); //open the file in append mode
fputs ( "This is an apple." , pFile );
fseek ( pFile , 0 , SEEK_END); // 0 means from start & SEEK_END will get you the end of file
fputs ( "Line you want to add:" , pFile );
fclose ( pFile );
return 0;
}
Upvotes: -1
Reputation: 2776
You wrote too much code. You need two lines only:
ofstream output("Sample.ini",ios::app);
output << outputstring;
Upvotes: 2