0

I have written a program that takes the filename from argv[1] and do operations on it . When debugging from visual studio I pass the filename from project options>>debugging>>command arguments and It works fine and prints all results correctly .

But when trying from the command prompt , I go to the dir of project/debug the I type

program

It works fine and prints "No valid input file" in the same window (Which is my error handling technique)

but when i type

program test.txt

It just does nothing . I think no problem in code because it works fine from the 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 , and a lot of variables ..
            //do a lot of stuff and while loops
          cout << currTime <<endl ;

}
    return 0 ;
    }
4
  • 2
    There's not enough here to go by. Obviously there is a problem with the code, or it would work, right? Commented Apr 29, 2011 at 15:05
  • I posted the code , though visual studio debugger prints the results fine . Commented Apr 29, 2011 at 15:10
  • I am inside the right directory , It printed "No valid input file" Commented Apr 29, 2011 at 15:12
  • 2
    if(argv[1]==NULL) should be replaced with if (argc != 2), add if/while (infile) before each IO operation. Commented Apr 29, 2011 at 15:18

3 Answers 3

3

You don't check if file was successfully opened, whether getline returned error code or not, or if string to integer conversion didn't fail. If any of those error occur, which I guess is the case, nLines will be equal to 0, no cycles will be performed and program will exit with return code 0.

Sign up to request clarification or add additional context in comments.

1 Comment

Well , I found the mistake which was that the test file in the project directory not the current directory so It worked for the debugger . So your answer is the most useful . I needed to check if file opened successfully or no .
3

This code worked correctly for me running on the command line.

#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) 
{ 
    int nLines;
    string str;

    if(argv[1]==NULL)
    {
        std::cout << "Not valid input file" << endl;
        return 0 ;

    }
    else
        std::cout << "Input file = " << argv[1] << endl;
}

Output :

C:\Users\john.dibling\Documents\Visual Studio 2008\Projects\hacks_vc9\x64\Debug>hacks_vc9.exe hello
Input file = hello

By the way, this code is dangerous, at best:

if(argv[1]==NULL)

You should probably be checking the value of argc before attempting to dereference a possibly-wild pointer.

1 Comment

Thanks for this note about argv.
1

The file probably contains an invalid numeric first line (perhaps starting with a space or the BOM).

That would explain no output, since if nLines == 0 no output should be expected

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.