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

C++ ofstream/ifstream读写文件demo

2018-01-27 17:05 671 查看
#include <iostream>
#include <fstream>
using namespace std;

int main(){
//1.ofstream写入out.txt
const char * filename = "test.txt";
string end= "123456";
ofstream out("out.txt");
if(out.is_open())
{
/*
"<<":插入器,向流输出数据.
">>":析取器,向流输出数据.
*/
out<<"Hello World." << filename  << " "<< end << endl;
out.close();
}

//2.读取out.txt
ifstream in("out.txt");
char buffer[200];
if(in.is_open())
{
while(!in.eof())
{
in.getline(buffer, 100);
cout << buffer << endl;
out.close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: