您的位置:首页 > 编程语言 > C语言/C++

C++读写txt文件

2013-12-09 10:04 381 查看
code:

#include <iostream>
#include <fstream>
#include<string>
using namespace std;

int main () {
ofstream ofile ("D:\\example.txt");
if (ofile.is_open())
{
ofile << "This is a line.\n";
ofile << "This is another line.\n";
ofile.close();
}
else cout << "Unable to open file";
string line;
ifstream ifile ("D:\\example.txt");
if (ifile.is_open())
{
while ( getline (ifile,line) )
{
cout << line << '\n';
}
ifile.close();
}

else cout << "Unable to open file";

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: