给你举个例子吧:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream wfile;
ifstream rfile;
//文件名称
string filename="123.txt";
string contents;
//打开文件
wfile.open(filename.
c_str());
if(wfile.is_open())
{
//写入文件,中间有空格的哈
wfile<<"Hello, my name is CSDN."<<endl;
}
wfile.close();
//从文件中读取内容,包含空格
rfile.open(filename.c_str());
if(rfile.is_open())
{
//读取一行
getline(rfile,contents);
}
//输出读出的内容,含有空格的哈
cout<<contents<<endl;
return 0;
}