Problem opening multiple files through command line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Myxamatosis
    New Member
    • Feb 2008
    • 30

    Problem opening multiple files through command line

    I'll skip the nitty gritty, but here's the outline of the program

    Code:
    int main( int Argc, char *Arg[])
    {
      ifstream indata;
      
      cout << "Command line contained " << Argc << " tokens\n\n";
      cout << "Name of executing file: " << Arg[0] << "\n";
    
      for (int I=1; I<Argc; I++)
        {
          cout << "Attempting to open " << Arg[I] << "..." << endl;
          
          indata.open(Arg[I]);
          
          if ( !indata )
          {
            cerr << "Error: File could not be opened"
          }
         
          else
          {
            (REST OF PROGRAM)
          }
        }
    }
    So when I run my code using only one file (test.txt, for example) the program works fine and does exactly what it needs to do.

    However, if multiple files are written in, say with a dummy file B
    a.out B test.txt

    B shows an invalid file...but so does test.txt, which we already know works.

    Also, something with two valid files
    a.out test.txt test1.txt

    this will simply run the first file no problem, but when it gets to the second file (simply a copy of test.txt) it reports that it's unable to open

    Is there something wrong with closing the files? I have close statements in the bulk section of the program

    Help is greatly appreciated,
    -Myxamatosis
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    if you are sure indata.close() is being called you might want to call indata.clear() before trying to open the next file - this will clear any error status bits which may have been set when you tried to open a file that didn't exist.

    http://www.cplusplus.c om/reference/iostream/ios/clear.html

    You may also want to consider adding some more error checking (but maybe you already have that and didn't include it in your stripped down example).

    Comment

    • Myxamatosis
      New Member
      • Feb 2008
      • 30

      #3
      indata.close() is definitely being closed, i've used a cout statement after the close statement, and it prints fine.

      Even with adding clear() I still seem to be getting the same problem. I've also changed (!indata) to (indata.fail())

      Comment

      • Myxamatosis
        New Member
        • Feb 2008
        • 30

        #4
        okay,

        So I moved the statements around a bit, and the program is now able to open multiple file commands that are valid . However, I'm back to the same problem when an invalid file is thrown in the mix

        a.out test.txt B test1.txt
        or
        a.out B test.txt

        anything after the invalid file is still regarded as invalid.

        if you need to see the rest of my code (it's lengthy) just ask.

        Comment

        • Myxamatosis
          New Member
          • Feb 2008
          • 30

          #5
          problem fixed. thanks again for the help

          Comment

          Working...