CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    9

    [RESOLVED] Processing multiple files from a single directory

    Hi,

    I've tried to form together the following code so that I can process multiple files from one user specified directory. However, when I run the program it works great for the first file, but all other files are blank. The problem starts on the following line:

    while(FileIn.good())

    If anyone can help me solve this, I'd be really greatful!

    //---------------------------------------------------

    #include <fstream>
    #include <iostream>
    #include <dirent.h>
    #include <sys/stat.h>

    using namespace std;

    struct dirent *dirp;
    struct stat filestat;
    string dir, ImportPath, ExportPath, LineData;
    DIR *dp;

    ifstream FileIn;
    ofstream FileSave;

    int main()
    {

    cout << "Please enter the directory to process: " << flush;
    getline(cin, dir);

    dp = opendir (dir.c_str() );
    if (dp == NULL)
    {
    cout << "Error(" << errno << ") opening " << dir << endl;
    return errno;
    }

    while ((dirp = readdir (dp)))
    { //A

    ImportPath = dir + "/" + dirp->d_name;

    if (stat( ImportPath.c_str(), &filestat)) continue;
    if (S_ISDIR( filestat.st_mode )) continue;

    ExportPath = dirp->d_name;

    FileSave.open(ExportPath.c_str() );

    FileIn.open(ImportPath.c_str() );

    cout << ExportPath;

    if(FileIn.is_open())
    { //B
    cout << " open" << endl;

    while(FileIn.good())
    { //C

    getline (FileIn, LineData);
    cout << LineData << endl;
    FileSave << LineData;

    //I have some more code here to control strings on each line
    } //C

    FileIn.close();

    } //B
    else
    {
    cout << "File is not open" << endl;
    }

    FileSave.close();

    } //A

    closedir( dp );

    system("PAUSE");
    return EXIT_SUCCESS;
    }

    //----------------------------------------------------

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Processing multiple files from a single directory

    Did you try to debug your code?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2012
    Posts
    9

    Re: Processing multiple files from a single directory

    Sorry here's the code in the right forum format.

    Thanks!

    Code:
    #include <fstream>
    #include <iostream>
    #include <dirent.h>
    #include <sys/stat.h>
    
    using namespace std;
    
    struct dirent *dirp;
    struct stat filestat;
    string dir, ImportPath, ExportPath, LineData;
    DIR *dp;
    
    ifstream FileIn;
    ofstream FileSave;
    
    int main()
    {
    
    cout << "Please enter the directory to process: " << flush;
    getline(cin, dir);
    
    dp = opendir (dir.c_str() );
    if (dp == NULL)
    {
    cout << "Error(" << errno << ") opening " << dir << endl;
    return errno;
    }
    
    while ((dirp = readdir (dp)))
    { //A 
    
    ImportPath = dir + "/" + dirp->d_name; 
    
    if (stat( ImportPath.c_str(), &filestat)) continue;
    if (S_ISDIR( filestat.st_mode )) continue;
    
    ExportPath = dirp->d_name; 
    
    FileSave.open(ExportPath.c_str() );
    
    FileIn.open(ImportPath.c_str() );
    
    cout << ExportPath;
    
    if(FileIn.is_open())
    { //B 
    cout << " open" << endl; 
    
    while(FileIn.good())
    { //C
    
    getline (FileIn, LineData);
    cout << LineData << endl;
    FileSave << LineData; 
    
    //I have some more code here to control strings on each line 
    } //C 
    
    FileIn.close(); 
    
    } //B
    else
    {
    cout << "File is not open" << endl; 
    }
    
    FileSave.close(); 
    
    } //A 
    
    closedir( dp ); 
    
    system("PAUSE");
    return EXIT_SUCCESS; 
    }

  4. #4
    Join Date
    Feb 2012
    Posts
    9

    Re: Processing multiple files from a single directory

    Hi Victor,

    Yes I'm using DevC++ and the debug goes through without any errors or warnings.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Processing multiple files from a single directory

    Quote Originally Posted by Simonkey View Post
    Yes I'm using DevC++ and the debug goes through without any errors or warnings.
    I never used DevC++, but any proper Debugger allow you to execute code step-by-step (line-by-line) and to see the values of variables around the place of execution.
    Victor Nijegorodov

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Processing multiple files from a single directory

    Quote Originally Posted by Simonkey View Post
    Sorry here's the code in the right forum format.
    No, it is NOT the right forum format.
    The right forum format assume the code have proper indentations, not just the grey background with the header "Code:".
    Victor Nijegorodov

  7. #7
    Join Date
    Feb 2012
    Posts
    9

    Re: Processing multiple files from a single directory

    Third time lucky :

    Code:
    #include <fstream>
    #include <iostream>
    #include <dirent.h>
    #include <sys/stat.h>
    
    using namespace std;
    
    struct dirent *dirp;
    struct stat filestat;
    string dir, ImportPath, ExportPath, LineData;
    DIR *dp;
    
    ifstream FileIn;
    ofstream FileSave;
    
    int main()
    {
    
    cout << "Please enter the directory to process: " << flush;
    getline(cin, dir);
    
    dp = opendir (dir.c_str() );
    if (dp == NULL)
       {
          cout << "Error(" << errno << ") opening " << dir << endl;
          return errno;
       }
    
    while ((dirp = readdir (dp)))
    { //A   
           
    ImportPath = dir + "/" + dirp->d_name;             
           
      if (stat( ImportPath.c_str(), &filestat)) continue;
      if (S_ISDIR( filestat.st_mode ))        continue;
            
    ExportPath = dirp->d_name; 
              
    FileSave.open(ExportPath.c_str() );
    
    FileIn.open(ImportPath.c_str() );
    
    cout << ExportPath;
    
          if(FileIn.is_open())
          { //B                     
                  cout << " open" << endl; 
                     
                 while(FileIn.good())
                 { //C
                 
                      getline (FileIn, LineData);
                      cout << LineData << endl;
                      FileSave << LineData; 
                      
                      //I have some more code here to control strings on each line 
                                   
                 } //C    
                                            
                  FileIn.close();                                 
                                                                         
         } //B
         else
         {
          cout << "File is not open" << endl;   
         }
         
    FileSave.close();   
          
    } //A   
    
    closedir( dp );   
    
    system("PAUSE");
    return EXIT_SUCCESS;   
    }

  8. #8
    Join Date
    Feb 2012
    Posts
    9

    Smile Re: Processing multiple files from a single directory

    Hi Victor,

    I usually use cout lines to isolate problems if the debugger doesn't throw back any warnings, but I understand that this is probably bad practice. I'm a casual C++ user, and only really use it on odd occasions.

    Would you be able to recommend a non-visual C++ compiler, with a good debugger?

    Thanks

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Processing multiple files from a single directory

    Quote Originally Posted by Simonkey View Post
    Would you be able to recommend a non-visual C++ compiler, with a good debugger?
    No, sorry!
    since the last 12 years I use only Microsoft IDEs...
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Processing multiple files from a single directory

    Quote Originally Posted by Simonkey View Post
    I usually use cout lines to isolate problems if the debugger doesn't throw back any warnings,
    As others have stated, the debugger allows you to run your program a single step at a time. It also allows you to watch variables as each line is executed. It's one of first pieces of a compiler suite that programmers look for -- the debugging facilities.

    Here are the problems with cout:

    You have to change your program every time you need to "cout" something new. Then you would need rebuild, then run, then if there is a problem, put another cout, rebuild, etc. What if your program was not small, but multiple source files? Where do you start with placing cout's? What if the build time is long instead of seconds?

    Secondly, you need to now remove all of those cout statements once you're done. How do you know you removed all of them? How do you know that removing them didn't introduce a bug somewhere (because once you touch the source code and add/remove lines, you can potentially introduce a bug).

    Last, it is possible that what you're going to "cout" can crash your program. For example, if you attempted to dereference an invalid pointer:
    Code:
    SomeType *p;
    //...
    cout << *p;
    If p is not initialized, then all bets are off if as to whether that cout statement will be executed properly.

    So learn to use the debugger. It is practically mandatory to learn to use it, as you can't write anything beyond toy programs without it (and even toy programs may need to be debugged).

    but I understand that this is probably bad practice. I'm a casual C++ user, and only really use it on odd occasions.
    But the code you posted would practically require one of us to debug it using the debugger. So you should be using the very same tools we are using to help you solve the problem.
    Would you be able to recommend a non-visual C++ compiler, with a good debugger?
    First, you are already using a non-Visual C++ compiler, namely g++. That is the compiler that Dev-C++ is using under the hood, and it is an industry standard C++ compiler. The debugger that g++ uses is gdb, a good (but hard to use as-is) debugger.

    The problem is that the Dev-C++ IDE has not been maintained in several years. Instead, get CodeBlocks as the IDE, along with the g++ compiler. You get the visual interface to the gdb debug facilities making gdb easier to use. In addition, you can get the DDD debugger which "visualizes" the gdb debugger.

    http://www.gnu.org/software/ddd/

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 21st, 2012 at 01:05 PM.

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Processing multiple files from a single directory

    The debugger doesn't give you any warnings, that's the compiler.
    Why does it have to be non-visual (interpreted as no gui)? MSVC contains a very good debugger.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  12. #12
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Processing multiple files from a single directory

    MSVC express is free as well. http://www.microsoft.com/visualstudi...itions/express
    As far as I understand it DevC++ isn't maintained anymore.
    Last edited by S_M_A; February 21st, 2012 at 12:59 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  13. #13
    Join Date
    Feb 2012
    Posts
    9

    Smile Re: Processing multiple files from a single directory

    Thanks very much Victor/SMA, I haven't seen these express versions before. I'll give them a go.

  14. #14
    Join Date
    Feb 2012
    Posts
    9

    Smile Re: Processing multiple files from a single directory

    Thanks Paul, really appreciate the info! I'll take all of this on board and get to grips with the debugging properly.

  15. #15
    Join Date
    Feb 2012
    Posts
    9

    Resolved Re: Processing multiple files from a single directory

    I worked out what the problem was on this issue. I needed to add a...
    Code:
    FileIn.clear();
    FileSave.clear();
    .. so that it would recognise the next file. Just applied it to 700 files and works great. Thanks to all for the advice on debugging and compiler options.

    Cheers

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured