How to get data in file before the end of long execution time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patienceBG
    New Member
    • May 2010
    • 4

    #1

    How to get data in file before the end of long execution time

    My C++ program made calculation several days and after electicity stop I lose all data. I try to print the data in intermediate files for every moment of interest and close these files with filename.close( ); but all data continue to appear in the text file (in the windows explorer) after the end of the program or after I stop the execution. Obviously the data stay in some buffer and I don’t know how to visualize them before the end.
    Thanks!
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Please clarify:
    "but all data continue to appear in the text file"

    Did you want to edit the text file somehow?
    How do you want to visualize the data?
    How is the data written in the files?

    Comment

    • patienceBG
      New Member
      • May 2010
      • 4

      #3
      Hi, jkmyoung, thanks for the attention.
      How is the data written in the files?
      ofstream filename;
      filename.open(" filename.txt", ios_base::out);
      if (!filename) {cout<<" filename !open\n"; return 1;}
      filename<< …<<"\n";
      filename.close( );
      How do you want to visualize the data? – Numbers in text file.
      Did you want to edit the text file somehow? Yes, I copy the numbers from the text file and analyze them in Excel.
      All works fine but I can access the data after the end of the program or after I stop the execution and this is to long and I already lose data from several days calculation.

      I find some solution but is ridiculous. I make the program to print useless data parallel with the needed, this fill this stupid buffer, which I don’t know where is, and the data appear in the file in the windows expoler before the end of the execution. I sort them in Excel and discard the junk.
      Thanks again!

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Have you considered using different filenames? Eg
        filename1.txt
        filename2.txt
        filename3.txt

        I suggest having a timer somewhere in whatever loop you're using to output the data and switch files.

        so have something like:
        Code:
        time_t timerStart = time();
        and in the output loop:
        if (time() - timerStart > 3600){ // about every hour.
          flush output stream.
          close file.
          rename file.
          reopen new file at original location.
          ...
          timerStart = time(); // reset start time
        }
        Just to clarify, you are doing the calculations manually, with human intervention? It's not fully automated, correct?

        Comment

        • patienceBG
          New Member
          • May 2010
          • 4

          #5
          Yes, I use timer and multiples files for some cycles and logic to protect them form reopening, but I never used “flush output stream” and I think, that this is what I need. I don’t know how exactly to use “flush”, but I shall read in internet. Thanks man.
          Yes, the calculations are not fully automated, i shall need more than 100 years if they are. I look in the data, use my intuition, change some coefficients and start again, but even like this i need weeks for one analysis and they are hundreds. I work on new metod for automatic speech recognition and after 12 years dashing my head against the wall, finaly I have good results. The human speech is terrible thing.
          Thanks again!

          Comment

          • patienceBG
            New Member
            • May 2010
            • 4

            #6
            It is simple “ filename<<flush ; “ and it works. Thanks, jkmyoung! God bless you!

            Comment

            Working...