我是一个完整的 C++ 新手(我只知道 Python),我正试图找出我做错了什么。本教程向我展示了如何输出文本文件 ( http://www.cplusplus.com/doc/tutorial/files/ )
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
然后当我尝试应用它时,它给了我一个分割错误
// basic file operations
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string WriteTXT( string DATA , string F_NAME)
{
ofstream myfile;
myfile.open (F_NAME.c_str());
myfile << DATA;
myfile.close();
}
string ReadTXT( string F_NAME )
{
string line;
ifstream myfile (F_NAME.c_str());
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
}
int main () {
WriteTXT("12345","example.txt");
ReadTXT("example.txt");
return 0;
}
到底怎么回事?