|
-
September 9th, 2002, 08:05 AM
#1
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!
-
September 9th, 2002, 08:31 AM
#2
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
-
September 9th, 2002, 10:05 PM
#3
Wow! open() will return an error if a file does not exist.
Kuphryn
-
September 10th, 2002, 05:16 AM
#4
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!
-
September 10th, 2002, 06:24 AM
#5
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
-
September 10th, 2002, 06:57 AM
#6
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?
-
September 10th, 2002, 07:31 AM
#7
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
-
September 10th, 2002, 07:58 AM
#8
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!
-
September 10th, 2002, 08:09 AM
#9
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
-
September 10th, 2002, 08:12 AM
#10
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;
}
-
September 10th, 2002, 08:59 AM
#11
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
-
September 10th, 2002, 09:38 AM
#12
Have you tried creating a new project and trying a small test
program yet?
-
September 10th, 2002, 09:50 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|