user18453148
user18453148

Reputation:

I am trying to get file to open while also compiling on same line so getting ./a.out <filename> but my file isnt opening

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

Upvotes: 0

Views: 34

Answers (1)

pm100
pm100

Reputation: 50210

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

  iFile.open(argv[1]);

Upvotes: 2

Related Questions