delorie.com/djgpp/bugs/show.cgi   search  
Bug 000057

When Created: 02/09/1996 12:44:54
Against DJGPP version: 2.00.beta5
By whom: elric@wheel.dcn.davis.ca.us or jtaylor@optivision.com (work)
Abstract: in|out stream truncated.
fstream m_file;  // member variable
m_file.open("abc", ios::in | ios::out | ios::nocreate);
file is truncated on open (is exists and is has text lines)
Workaround: open ios::in | ios::nocreate to read, close,
open ios::out | ios::trunc to write.

Solution added: 10/20/1996 04:47:38
By whom: Hellwig.Geisse@mni.fh-giessen.de
Problem:  existing file truncated on open

Example:  fstream m_file;
          m_file.open("abc", ios::in | ios::out | ios::nocreate);

Solution: add ios::app to the file mode

Example:  fstream m_file;
          m_file.open("abc", ios::in | ios::out | ios::nocreate | ios::app);

Note: The reason for truncation of the existing file is the algorithm
which determines the state of the O_TRUNC flag on opening the file. It
goes roughly like that:

      if ((mode & ios::trunc) ||
          (!(mode & ios::app) && (mode & ios::out))) 
        open_mode |= O_TRUNC;

I think that there is nothing wrong with this algorithm. Simply add the
ios::app flag to the file mode and the file will remain intact.

Note added: 10/28/1996 10:45:29
By whom: branko.drevensek@uni-mb.si
>Hellwig.Geisse@mni.fh-giessen.de 
...
>      if ((mode & ios::trunc) ||
>          (!(mode & ios::app) && (mode & ios::out))) 
>        open_mode |= O_TRUNC;
>
>I think that there is nothing wrong with this algorithm. Simply add the 
>ios::app flag to the file mode and the file will remain intact.

ios::app seeks stream to end of file before each write, so you can't write 
somewhere in the middle this way.

Bug is fixed in libg++ version 2.7.1:
>  if ((mode & (int)ios::trunc) ||
>      ((!(mode & (int)(ios::app||ios::in))) && (mode & (int)ios::out)))
>    posix_mode |= O_TRUNC;
"app" and "in" streams are not truncted anymore.

Libg++ version 2.7.2 (not available in DJGPP distribution yet) changes this to:
>  if ((mode & (int)ios::trunc) || mode == (int)ios::out)
>    posix_mode |= O_TRUNC;
,so only pure "out" streams are truncated without request.

Closed on 04/13/1999 09:00:29: This bug was in the C++ library, and is solved in later releases.
By whom: eliz@is.elta.co.il



  webmaster     delorie software   privacy  
  Copyright © 2010   by DJ Delorie     Updated Jul 2010