tags:

views:

132

answers:

2

I've been scratching my head for quite some time now, this code worked fine when I first used cmd to go inside the project\debug folder then run the program there. Then I added the if(in) and else part then it started giving me "debug assertion failed" errors mbstowcs.c

Expression s != NULL

It just doesn't make any sense to me..

I used this command in cmd: prog.exe test.txt nuther.txt

Both files exists inside the debug folder and the main project folder..

Any ideas?

    int main(int argc, char **argv)
        {
        parse_opts(argc, argv); //parse the arguments

        return 0;
    }


    void parse_opts(int argc, char **argv)
    {
        string compl_out;

        if( argc > 1 )
        {
         for( int i = 1; i < argc; i++ )
         {
          if( argv[i][0] = '>' )
          {
           ofstream out_file(argv[i+1]);
           out_file << compl_out;
           out_file.close();
           break;
          }

          ifstream in(argv[i]);
          string buff;

          if(in)
          {
           while(getline( in, buff ))
           cout << buff << endl;

           compl_out.append(buff); 
          }
          else
          {
           cout << "Can't open file: " << argv[i] 
             << ", file doesn't exist or is locked in use. " << endl;
          }
         }
        }
        else
        {
         usage();
        }

}
+2  A: 

First impressions:

if( argv[i][0] = '>' )

should be:

if( argv[i][0] == '>' )

You are assigning instead of comparing.

I think you also might have intended the compl_out.append to be inside the while loop? As it is it won't append anying to that buffer:

while(getline( in, buff ))
{
    cout << "buf" << buff << endl;
    compl_out.append(buff); 
}
Cannonade
Yeah, I immediately saw that after I posted.. LOL Stupid mistakes like that are soo frustrating..
Charles Khunt
Good stuff ... Not sure why you are getting the assertion. Unless I missed something I would expect the program to just exit given those params. Though if you passed "blah.exe >" it could crash on the ofstream out_file(argv[i+1]); line.
Cannonade
A common "trick" is to write the constant expression on the right side, if you make that a habit it may save you some trouble. E.g. if ( '>' == arg[i][0] ) would be ok, if ( '>' = arg[i][0] ) would give compiler error.
Anders K.
@Anders That is a cool trick. I wonder if my virtual muscle memory is too ingrained to adopt that practice.
Cannonade
A: 

Is 'in' a C++ keyword?

Shakedown
nope, it's not.
Charles Khunt