我想在现有文件的末尾添加一个新行(字符串)。但它没有用。这是代码:
#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;
}
将文件内部的行读取到字符串的第一个getline运行良好。但是,第二个,不是。 编译器返回如下:
... \ file test.cpp | 35 | error:没有匹配函数来调用&#39; getline(std :: istream&amp;,std :: ofstream&amp;)&#39; |
答案 0 :(得分:2)
你写了太多代码。你只需要两行:
ofstream output("Sample.ini",ios::app);
output << outputstring;
答案 1 :(得分:-1)
你可能想尝试poniter
提交,简短和&amp;简单
#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;
}