我曾生成一个TXT文件并写了内容,现在希望用C++文件操作把它打开,并且把里面的全部内容打印到屏幕上

请问代码怎么写可以实现?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " filename.txt" << endl;
return -1;
}

ifstream ifs(argv[1]);
if (ifs.fail())
{
cout << "open " << argv[1] << " failed" << endl;
return -2;
}

while (true)
{
string line;
getline(ifs, line);
if (!ifs.eof())
cout << line << endl;
else
break;
}
ifs.close();

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-08
FILE *fp;

unsigned char ch;

fp=fopen("路径");
if(fp!=NULL)
{
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);

}
相似回答