Click to See Complete Forum and Search --> : How to poll a file using STL?!?!


Fusion
September 9th, 2002, 08:05 AM
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!

PaulWendt
September 9th, 2002, 08:31 AM
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/showthread.php?s=&threadid=179459&highlight=fstat

--Paul

kuphryn
September 9th, 2002, 10:05 PM
Wow! open() will return an error if a file does not exist.

Kuphryn

Fusion
September 10th, 2002, 05:16 AM
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!

cup
September 10th, 2002, 06:24 AM
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.

Fusion
September 10th, 2002, 06:57 AM
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?

PaulWendt
September 10th, 2002, 07:31 AM
Post your code; I can use stat() just fine. Here's an example
program from Microsoft's webpage:

#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

Fusion
September 10th, 2002, 07:58 AM
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!

PaulWendt
September 10th, 2002, 08:09 AM
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

Philip Nicoletti
September 10th, 2002, 08:12 AM
How about this ?


#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;
}

Fusion
September 10th, 2002, 08:59 AM
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 :mad:

PaulWendt
September 10th, 2002, 09:38 AM
Have you tried creating a new project and trying a small test
program yet?

Fusion
September 10th, 2002, 09:50 AM
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!!!