编写一个文本文件读取程序,打开 test.txt 文件,读取所有内容并显示在屏
幕上。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("text.txt",ios::in);
if(!file.fail())
{
while(!file.eof())
{
char buf[128];
file.getline(buf,128);
if(file.tellg()>0)
cout<<buf<<endl;
}
}
else
cout<<"can not open"<<endl;
file.close();
return 0;
}
这是一个使用C++编程语言编写的程序,用于读取名为'test.txt'的文本文件内容并将其显示在屏幕上。程序通过fstream库实现文件操作,检查文件是否成功打开,并逐行读取内容直至文件结束。
213

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



