Saturday, 15 January 2011

c++ - Is file ready to read immediately after std::ofstream is destroyed? -


Actually I'm following workflow (via console application):

  • Read the binary file (< std :: ifstream :: read )
  • Do something with the read data
  • The same file ( std: Now, if I run the whole console program 1000 times (always using the same file) through a shell script, then believe it. Safe to read that pre-walker Trying to write a pathogram is trying to write? Or do I have to wait between hanging (how long ???)? Can I firmly determine that the file is ready?

    Know that this is not the best design, just wants to know that it will work reliably (trying to gather some figures quickly - the input is different, but the output file is always the same - the need to read Is information Programming needs to be processed, then you need to update it (just write it at this point)).

    Edit:

    The problem seems to be incorrect regarding the output, not related to the OS based on the answers, I see it in reading / writing Is:

      // read std :: ifstream input (fname), std :: ios_base :: binary); While (input) {unsigned value; Input.read (reinterpret_cast & lt; four * & gt; (& value), sizeof (unsigned)); ....} input.close (); ... // write std :: offstream output (fname, std :: ios_base :: binary); (Std :: map & lt; unsigned, unsigned & gt; :: const_iterator iter = originalMap.begin (); iter! = OriginalMap.end (); ++ iter) {unsigned temp = iter-> first; Output.write (reinterpret_cast & lt; four * & gt; (& temp), sizeof (unsigned)); Temp = iter-> second; Output.write (reinterpret_cast & lt; four * & gt; (& temporary), sizeof (unsigned)); }    

    They are running sequentially, essentially, open The script runs the same console app in a loop ...

    Then, there should be no problem on any "normal" operating system.

    When the application is finished then the streams are destroyed, so all the data kept in the cache by the C / C + streams are written in the underlying OS currents, which are then closed.

    Is the OS doing something else caching is irrelevant - the caching system operated by is transparent for applications, so, as far as the applications are concerned, the data is now written in the file. If this is actually written on the disc then there is no worry - data will be visible in the file-reading application.

    If you think about it, without any guarantee like this, it will be complicated to do any work reliably on a computer! :)


    Update:

      std :: offstream output (fname, std :: ios_base :: binary);    

    You must leave the output file before writing it, otherwise, if the input file was longer than the output, the old data will still be at the end of the file:

      std :: offstream output (fname, std :: ios_base :: binary | std :: ios_base :: trunc);    

No comments:

Post a Comment