How to pause the code untill an external file is created then continue?


Hello everyone,

I m writing a console program that launches other applications one after the other, namely that the file produced by the first application is going to be used by the second and so on..

void main(void)
{
CreateProcess(L"D:\\blabla0.bat",NULL,NULL,NULL,FALSE,0,NULL,NULL,&si, &pi);

// this process produces a file "D:\\XX.mb"
//here I want my code to wait until the file "D:\\XX.mb" is created

CreateProcess(L"D:\\blabla1.bat",NULL,NULL,NULL,FALSE,0,NULL,NULL,&si, &pi);
}

does anyone know how pause a c++ code untill an external file is created?

Thank you very much,
When I run into this issue and the workaround that I use is to, in this case, have blabla0.bat call a third program at the end of it's execution that third program is what processes the data from XX.mb.

The problem is that CreateProcess returns from the OS as soon as the app you called is started.

Otherwise you can do a lot of things in C\C++ that you can with bat files maybe your solution is to rewrite the bat file in C\C++?

EDIT: Pausing a program until a condition is met is really a loop that keeps testing for that condition. When the condition is met the otherwise infinite loop exits allowing the rest of the program to continue processing. In this case you would use the openfile function (who's name escapes me at the moment) and just capture and throw out the fail, exiting the loop once openfile returns true.

REEDIT: Here we go, I couldn't think of it because I forgot you can use the CreateFile function to open an existing file.
http://msdn.microsoft.com/en-us/library/bb540534(VS.85).aspx
Enjoy
Last edited on
Thanks a lot for your reply,

The .bat file is important because it is read by the other application later (mayabatch) anyway..
i did make a an while loop checking the existance of the new file yesterday (not using the EDIT function though; i ll give that a try today);
meanwhile i wanted to show you the loop i made that didnt work:

void main(void)
{
CreateProcess(L"D:\\blabla0.bat",NULL,NULL,NULL,FALSE,0,NULL,NULL,&si, &pi);

// this process produces a file "D:\\XX.mb"

string XX="D:\\XX.mb";
int exists=0;
ifstream ifile(XX);

while (exists==0)
{
if (ifile)
{exists=1; CreateProcess(L"D:\\blabla1.bat",NULL,NULL,NULL,FALSE,0,NULL,NULL,&si, &pi); }
else
{exists=0;}
cout<<exists;
}

}


in principle this should work fine !!!
so during the first round of the while loop the XX.mb file is not created yet so the ifile()function is returning nul, that's ok.
but even when the file is created the later rounds of the while loop, the ifile() function fails to detect the XX.mb file.

Do you have any idea why? is there a refresh action needed or something?
thanks a lot,

Topic archived. No new replies allowed.