我编写了一个打开文件的程序,然后逐行显示其内容(文本文件)
#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;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要添加什么才能使文件成为只读文件?
(infile.open(argv[1])我猜哪里去了)
cni*_*tar 13
该课程ifstream仅供阅读,问题解决了.另外,你真的打算argc 在使用后检查argv[1]吗?
另一方面,当您使用时,fstream您需要指定打开文件的方式:
fstream f;
f.open("file", fstream::in | fstream::out); /* Read-write. */
Run Code Online (Sandbox Code Playgroud)