我编写了一个程序,它打开一个文件,然后逐行显示其内容(文本文件)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (int argc, char* argv[])
{
string STRING;
ifstream infile;
infile.open(argv[1]);
if (argc != 2)
{
cout << "ERROR.\n";
return 1;
}
if(infile.fail())
{
cout << "ERROR.\n";
return 1;
}
else
{
while(!infile.eof())
{
getline(infile,STRING);
cout<<STRING + "\n";
}
infile.close();
return 0;
}
}
我需要添加什么来使文件只读?
(我猜测应该在
infile.open(argv[1])
中添加一些内容。)
while(!infile.eof())
,而应该使用while(getline(infile,STRING))
。 - Bart