#include <iostream>
#include <fstream> //这个头文件是必须要有的
using namespace std;
int main()
{
//输出,写入到文件(文件是外部设备)
ofstream fout("D:\\data.txt"); //创建一个data.txt的文件
fout << "hehehe" << "," << 22 << ","<< 3.1314 <<","<<"哈哈"<< endl; //将变量的值写入文件
fout.close(); //关闭文件
//读取文件
ifstream fin("D:\\data.txt"); //源
if (!fin) //判断读取是否成功
{
cerr << "打开失败" << endl;
return -1;
}
char s;
while (!fin.eof()) //逐个字符读入(包括空格与回车),中英文,数字都可以
{
fin >> s;
cout << s;
}
cout << endl;
system("pause");
return 0;
}
输出,写入到外部文件ofstream,读取外部文件ifstream
最新推荐文章于 2025-05-17 19:45:00 发布