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

1

There are 1 best solutions below

1
On

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

  iFile.open(argv[1]);