我正在尝试使文件打开,同时还要在同一行上进行编译,从而获取./a.out< filename>但是我的文件没有打开

发布于 2025-01-17 09:41:04 字数 841 浏览 2 评论 0原文

我能够获得允许我输入 ./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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2025-01-24 09:41:04

您没有使用argv [],那就是参数所在。你需要

  iFile.open(argv[1]);

You have done nothing with argv[], thats where the arguments are. You need

  iFile.open(argv[1]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文