Running a program from command prompt and using argv in C++ -


i have written program takes filename argv[1] , operations on . when debugging visual studio pass filename project options>>debugging>>command arguments , works fine , prints results correctly .

but when trying command prompt , go dir of project/debug type

program 

it works fine , prints "no valid input file" in same window (which error handling technique)

but when type

program test.txt 

it nothing . think no problem in code because works fine debugger .

code :

int main(int argc, char *argv[])   {  int nlines; string str;  if(argv[1]==null) {     std::cout << "not valid input file" << endl;     return 0 ;  } ifstream infile(argv[1]);  getline(infile,str); nlines = atoi(str.c_str());//get number of lines  for(int line=0 ;line < nlines;line++) {     //int currtime , , lot of variables ..             //do lot of stuff , while loops           cout << currtime <<endl ;  }     return 0 ;     } 

you don't check if file opened, whether getline returned error code or not, or if string integer conversion didn't fail. if of error occur, guess case, nlines equal 0, no cycles performed , program exit return code 0.


Comments