C++中:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string s;
ifstream fp("test.txt");
if (!fp)
{
cerr << "OPEN ERROR" << endl;
return 1;
}
while (getline(fp,s))
{
cout << s << endl;
}
fp.close();
return 0;
}
MFC中:
#include <afx.h>
#include <stdio.h>
void main()
{
CString buff;
CStdioFile cFile;
BOOL bOpen = cFile.Open("1.txt", CFile::modeRead);
if(bOpen)
{
while(cFile.ReadString(buff))
{
printf("%s\n", buff);
}
}
}
本文提供了一个使用C++标准库与MFC框架读取文本文件的示例程序。C++部分展示了如何利用`ifstream`与`getline`函数逐行读取文件内容并打印;MFC部分则通过`CStdioFile`类实现类似功能。
1461

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



