CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jan 2001
    Posts
    29

    Question How to poll a file using STL?!?!

    Hi all! I'm having difficulties polling for a file to exist cq. get opened... the code is simillar to:

    std::ifstream lv_InFile;

    lv_InFile.open("blabla");

    while (!lv_InFile.is_open() && nTimeOut--)
    {
    Sleep(1000);

    lv_InFile.open("blabla");
    }
    if (!lv_InFile.is_open())
    return false;

    It only sees the file if it's there from the beginning, not if it gets copied somewhere during nTimeOut seconds?

    Does anyone know of a neat algorithm to poll for a file using STL? Thanks in advance!

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I wouldn't use ifstream of ofstream to see if a file exists. There's
    no real need to open the file; check out this old thread:
    http://www.codeguru.com/forum/showth...ighlight=fstat

    --Paul

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    Wow! open() will return an error if a file does not exist.

    Kuphryn

  4. #4
    Join Date
    Jan 2001
    Posts
    29
    I know, but the code above (which is based upon both open() and is_open returning an error if the file does not exists) DOES NOT WORK!

    I'll check out the stat() function and let you guys know. Thanks for you replies!

  5. #5
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Special note on stat: the structure it uses is also called stat. Remember to prefix it with struct otherwise the compiler may get really confused.
    Succinct is verbose for terse

  6. #6
    Join Date
    Jan 2001
    Posts
    29
    Ok. stat() also crashes, way deep down in NTDLL!! I get a "PAGEHEAP: Bad heap handle" error message. But MSDN of any other MS website gives me NO CLUE WHATSOEVER. I already heard of problems with STL using VC++, but I thought that stat is part of the normal C library?! Anybody has any experiences simillar to this, or any clues?

  7. #7
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Post your code; I can use stat() just fine. Here's an example
    program from Microsoft's webpage:
    Code:
    #include <time.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    
    void main( void )
    {
       struct stat buf;
       int result;
       char buffer[] = "A line to output";
    
       /* Get data associated with "stat.c": */
       result = stat( "str_block.cpp", &buf );
    
       /* Check if statistics are valid: */
       if( result != 0 )
          perror( "Problem getting information" );
       else
       {
          /* Output some of the statistics: */
          printf( "File size     : %ld\n", buf.st_size );
          printf( "Drive         : %c:\n", buf.st_dev + 'A' );
          printf( "Time modified : %s", ctime( &buf.st_mtime ) );
       }
    }
    stat() is part of the C library, but the C library is also part of
    the C++ standard library. Remember that C++ kept C so that we
    could have backwards mantainability with old programs.

    --Paul

  8. #8
    Join Date
    Jan 2001
    Posts
    29
    OK my code is the following now:

    std::ifstream lv_InFile;
    struct stat lv_Status;
    bool lv_bFileExists = ::stat("blabla", &lv_Status) == 0;
    while (!lv_bFileExists && nTimeOut--)
    {
    Sleep(1000);

    lv_bFileExists = ::stat("bla", &lv_Status) == 0;
    }
    if (!lv_bFileExists)
    return false;

    lv_InFile.open(GetFileName());

    It totally breaks down at the first call to stat()! It says "PAGEHEAP: Bad heap handle"?!?! When searching MSDN or WWW it is of no help at all?!?!

    Do you have any special project settings or something Paul? Thankx for all your help so far!

  9. #9
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Your code looks fine to me and I don't have any special project
    settings that I know of. I'd try to take the program I posted
    earlier and start your own, new project. If you don't get a problem
    in your new project, you know that you've got a problem
    somewhere else. If you do get a problem still, then it'd appear to
    be a problem with your stat().

    Do you have any service packs for Visual Studio?
    I'm running WinNT4.0 SP6a with Visual Studio 6.0 SP5.

    --Paul

  10. #10
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    How about this ?

    Code:
    #include <fstream>
    #include <iostream>
    #include <windows.h>    // #include <unistd.h> // on most unix systems
    
    using namespace std;
    
    bool file_exists(string& filename , int nTimeOut);
    
    int main()
    {
        string filename = "test.dat";
        if (file_exists(filename,10))
            cout << "file exists" << endl;
        else
            cout << "file does not exist" << endl;
    
        return 0;
    }
    
    bool file_exists(string& filename , int nTimeOut)
    {
        for (int i=0; i<nTimeOut; ++i)
        {
            ifstream infile(filename.c_str());
            if ( infile )
                return true;
            Sleep(1000);    //  sleep(1); // on most unix systems
        }
    
        return false;
    }

  11. #11
    Join Date
    Jan 2001
    Posts
    29
    Hi Philip, tried you solution and it crashes on the line:

    std::ifstream infile("blabla");

    Paul, I have the same SPs installed also.

    I'm pretty sure that this is NOT some strange memory corruption error or something, it really seems to go wrong somewhere deep down in the STL or C library stuff in NTDLL....

    I'm at a loss

  12. #12
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Have you tried creating a new project and trying a small test
    program yet?

  13. #13
    Join Date
    Jan 2001
    Posts
    29
    Just did it and it works fine... I'm now gradually adding stuff (3rd party libs, dlls and so on) to see what causes the problems... thanks for your time Paul!!!

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