包含头文件
#include <iostream>
#include <fstream>
输出数据到txt
//创建日志文件
CString strTime;
CTime tm;
tm = CTime::GetCurrentTime();
strTime = tm.Format("%Y年%m月%d日%X.txt");
strTime.Replace(':', '_');
std::ofstream ofs;
ofs.open(strTime);//这种打开方式会覆盖原先数据,要不覆盖就要在后面加一个参数 ios::app
if(!ofs) //检查文件是否正常打开//不是用于检查文件是否存在
{
return;//打开失败
}
输出数据到文件
ofs << x << m_edit_NowAngle<<endl;
关闭文件
ofs.close();
输入数据到txt
ifstream ifs("1.txt", ios::in); // 以读取模式打开
if (!ifs)
{
return;
}
//方式1
//while (getline(ifs, line))
//{
// cout << line << endl;
//}
//方式2
int integer1, integer2;
string string1, string2;
ifs >> integer1 >> integer2 >> string1 >> string2;
ifs.close();
本文介绍了一个使用C++进行文件读写的示例。包括如何创建并输出数据到TXT文件,以及从TXT文件中读取数据的方法。通过具体代码展示了文件流的使用,如ofstream和ifstream,并解释了打开文件的不同模式。
564

被折叠的 条评论
为什么被折叠?



