我正在尝试使文件打开,同时还要在同一行上进行编译,从而获取./a.out< filename>但是我的文件没有打开
我能够获得允许我输入 ./a.out 然后输入文件名的代码 但是当我输入有效的文件名时它会继续输出 “错误:文件名无效” 不知道如何解决这个问题,
这是我到目前为止所做的,
#include <fstream>
#include <iomainop>
#include <iostream>
using namespace std;
int main(int argc, char*argv[]){
ifstream iFile;
string fname="";
if ( argc != 2 )
{
cout<<"usage: "<< argv[0]<< " <file name>" <<endl;
}
else
{
iFile.open(fname);
if (!iFile.is_open())
{
cout<<endl;
cout<<"Error: Invalid file name"<<endl;
cin.clear();
cin.ignore();
}
else
{
while (!iFile.is_open() )
{
cout<<"Opened";
}
}
}
iFile.close();
return 0;
}
我让它能够输入 ./a.out <文件名>
但是当我尝试打开文件时,它不断输出“错误:文件名无效”,我不确定为什么我的文件无法打开
i was able to get the code to allow me to enter ./a.out then a file name
but when i enter a valid file name it keeps outputting
"Error: invalid filename"
not sure how to fix this
this is what I've done so far
#include <fstream>
#include <iomainop>
#include <iostream>
using namespace std;
int main(int argc, char*argv[]){
ifstream iFile;
string fname="";
if ( argc != 2 )
{
cout<<"usage: "<< argv[0]<< " <file name>" <<endl;
}
else
{
iFile.open(fname);
if (!iFile.is_open())
{
cout<<endl;
cout<<"Error: Invalid file name"<<endl;
cin.clear();
cin.ignore();
}
else
{
while (!iFile.is_open() )
{
cout<<"Opened";
}
}
}
iFile.close();
return 0;
}
i got it to be able to type in
./a.out <filename>
but when i try opening a file it keeps outputing "Error: Invalid file name" im not sure why my file isnt opening
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有使用
argv []
,那就是参数所在。你需要You have done nothing with
argv[]
, thats where the arguments are. You need