| hjazz (3) | |||||||||
|
Hi, I'm using Visual C++ 6.0 and I'm trying to use fstream to open and read a file that is created only during runtime. This file is written by another function running on another thread, and my program will keep trying to "open" the file until it can be opened, i.e. after it's created, then read 3 numbers from it and execute the rest of its code. The file test.txt has the content
My program that polls and opens the file is as follows:
I executed the program by test_prog.exe "C:\test.txt"and waited about 3 seconds before putting the test.txt file into C:\ My output was
The test.txt file disappears after I refresh the C:\ folder. So the values for snum1, num2 and num3 are all wrong, as if the file was not read correctly. If I put a while fin.good() loop after printf("fin is open\n"); for that entire block (until printing the values of num2 and num3), then I get
How can I correctly read a file that is only created during runtime? Thank you. | |||||||||
|
Last edited on
|
|||||||||
| guestgulkan (2916) | |||
This line looks suspicious:
| |||
|
|
|||
| hjazz (3) | |||
|
Thanks for spotting the mistake. I've changed donefile to tfile, and still have the same problem. I've also added a
check right after the printf("fin is open\n");line, and the failbit was set when the file is put into the folder during runtime. Everything runs correctly when the file is in place before execution. ETA: I finally found the problem. The failbit was set after the first fin.open failed, and was never reset when the fin.open succeeded. Once I did a fin.clear() after Line 24 of my original post, the program ran successfully. | |||
|
Last edited on
|
|||
| ArlenLaub (1) | |
| dgt | |
|
|
|