0

Hi everyone sorry for the title of the question, I know there are a lot of questions regarding reading files in C++ but I wanted to check with you if it has a different approach if the files that I am reading are being generated at run-time.

So far I can read files no problem as long as the file exists. However, if I were to run the code and the files are being generated, it doesnt matter if I do the checking of the existence of the file, it just freezes my app. I am receiving the files from another process within my application.

Not sure what am I doing wrong so I apologize if the question is too vague. (The loop for reading the next file is in my main process)

std::ifstream infile;

while(1){
    infile.open(createFile(test, fileIndex), std::ios::in);
    if(infile.good())
        break;
}

if (infile.is_open()){

    int i = 0;
    while(infile >> i){
        //Here I am actually plotting i in a wxWidgets App
        //Using mpFXYVectors
    }

    Mathplot->SetData(AVG_X, AVG_Y);
    Mathplot->AddLayer(AVG);
    Mathplot->UpdateAll();
    Mathplot->Fit();
    infile.close();

}else{
   //Inform the user something went wrong
}

The loop is in another class and it is a simple

 while(1){

    readProcess->keepItGoing(fileIndex);
    fileIndex++;
    if(stop==1)
       break;
 }

Again my apologies if it is a foolish question but I have been stuck for a while now in a reading task that it is supposed to be simple.

Galvar
  • 57
  • 6
  • 2
    Don't use eof as a stream condition, ever. – Arnav Borborah Feb 14 '18 at 23:07
  • 1
    Possible duplicate of [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) –  Feb 14 '18 at 23:09
  • The file stream you use to open a file close it before using it again on another file. – Raindrop7 Feb 14 '18 at 23:10
  • `while(!infile.eof()){ infile >> i; }` Who taught you to do this? Shoot them in the face. – Lightness Races in Orbit Feb 14 '18 at 23:13
  • _(Disclaimer: Please do not shoot anybody on any part of their anatomy. The above comment is not a serious recommendation to cause bodily harm, violate weapons laws, or cause any damage to any person or property. Cheddar is better than brie.)_ – Lightness Races in Orbit Feb 14 '18 at 23:14
  • @LightnessRacesinOrbit I guess college profs are not the best example to follow.. – Galvar Feb 14 '18 at 23:18
  • You could monitor the length of the file and only read it if the length increases. Another idea is to use an OS API that allows you to read the file without locking it. – Thomas Matthews Feb 14 '18 at 23:20
  • Thanks guys I changed it now to the proper way mentioned on the possible duplicate question mentioned by @TheDude. However, it is still giving me a problem freezing my app. It seems to be able to read ONLY if I put a wxMessageBox right before opening the file to inform the user that a new file will be open. However, if I remove that or try to replace it with a pause of any kind it goes back to freezing the app.. – Galvar Feb 14 '18 at 23:21
  • @ThomasMatthews I will look into that right away thanks for the idea, cuz honestly it is not working. – Galvar Feb 14 '18 at 23:22
  • Concurrent file access is a messy business. Avoid it. Since it reads like you control both the writer and the reader, consider having the writer send a message to the reader when the file is complete and consumable. – user4581301 Feb 14 '18 at 23:23
  • @Galvar: Sadly they're not. – Lightness Races in Orbit Feb 14 '18 at 23:25

0 Answers0