#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char str[50];
ofstream outfile;
outfile.open("a.dat",ios::out); // 打开文件a.dat
if(!outfile) // 文件打开错误时,则退出
{
cout << "文件打开错误" << endl;
abort();
}
cout << "请输入字符串,以星号'*'结束;" << endl;
cin.getline(str,50,'*');
outfile << str; // 将输入的字符串存入文件a.dat中
outfile.close(); // 关闭打开的a.dat文件
char s[50];
ifstream infile;
infile.open("a.dat",ios::in); // 打开文件a.dat
if(!infile) // 文件打开错误时,则退出
{
cout << "文件打开错误" << endl;
abort();
}
infile >> s; // 从文件a.dat中读入字符串
cout << s << " "; // 显示读入的字符串
infile.close(); // 关闭打开的a.dat文件
cout << endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考