Writing EOF into an output stream

Posted on 2004-11-12
Medium Priority
Last Modified: 2013-11-20
Is there anyway to insert an EOF into an output stream to truncate the underlying file?

I have code that reads a five-line text file in with ifstream, closes it, opens a stream for output with ofstream on the same file, writes out the first four lines read in, and closes the output stream.  I want the file to be terminated after the fourth line I write back out, but the EOF for the file hasn't moved, so that displaying the file with notepad still shows all five lines.  How can I truncate this file at the end of my write?

Question by:steva
  • 2
  • 2

Expert Comment

ID: 12572352
Use seekg() and position the stream pointe,r where you want. Then close it.

Good Luck!

LVL 45

Expert Comment

ID: 12574479
ofstream( const char* szName, int nMode = ios::out, int nProt = filebuf::openprot );

ofstream( filedesc fd );

ofstream( filedesc fd, char* pch, int nLength );



The name of the file to be opened during construction.


An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR ( | ) operator. The nMode parameter must have one of the following values:

ios::app   The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.

ios::ate   The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.

ios::in   If this mode is specified, then the original file (if it exists) will not be truncated.

ios::out   The file is opened for output (implied for all ofstream objects).

ios::trunc   If the file already exists, its contents are discarded. This mode is implied if ios::out is specified and ios::ate, ios::app, and ios:in are not specified.

According to help you should be trashing the contents of the file with the ofstream UNLESS you specif y another flag that keeps the contents.  You should post some code.  Also try sending some other text to ofstream to see if the file is actually having anything written to it at all.

Author Comment

ID: 12580340
Andy, I think you may have hit on the problem when you said - "UNLESS you specify another flag that keeps its contents."

When I open the output stream on the file I write
    ofstream x_out(fileName, ios::in | ios::out);

After doing some experimenting, I see that  it's the ios::in flag that keeps the contents, i.e., keeps the file pointer at the end of the original file even though the put pointer is short of the end when I close the file. If I just use ios::out, or default (no flags) the EOF get's written after the last byte that I write to the file, and I truncate the file as I want to do.

But the answer isn't as simple as removing ios::in.  After much pain, I discovered last month that this is required to open this particular file for output because the file has "hidden" and "system" attributes. (It's actually boot.ini.)

I wrestled with this issue last month here at Experts Exchange, and after resolving that the ios::in | ios::out was needed to open a system/hidden file to an output stream in Windows, the best advice offered was that "hidden and system attributes were OS-specific and standard C++ does not provide tools to work with them."  No one knew why including the ios::in flag did the trick and opened the file for output.

So now I'm stuck.  I can't open the file for output without the ios::in flag but with the ios::in flag I can't shorten the file, as I need to do.

Does anyone see a way around this?


LVL 45

Accepted Solution

AndyAinscow earned 500 total points
ID: 12581697
BOOL GetStatus( CFileStatus& rStatus ) const;

static BOOL PASCAL GetStatus( LPCTSTR lpszFileName, CFileStatus& rStatus );

Return Value

TRUE if the status information for the specified file is successfully obtained; otherwise, FALSE.



A reference to a user-supplied CFileStatus structure that will receive the status information. The CFileStatus structure has the following fields:

CTime m_ctime   The date and time the file was created.

CTime m_mtime   The date and time the file was last modified.

CTime m_atime   The date and time the file was last accessed for reading.

LONG m_size   The logical size of the file in bytes, as reported by the DIR command.

BYTE m_attribute   The attribute byte of the file.

char m_szFullName[_MAX_PATH]   The absolute filename in the Windows character set.

A string in the Windows character set that is the path to the desired file. The path can be relative or absolute, but cannot contain a network name.


The virtual version of GetStatus retrieves the status of the open file associated with this CFile object. It does not insert a value into the m_szFullName structure member.

The static version gets the status of the named file and copies the filename to m_szFullName. This function obtains the file status from the directory entry without actually opening the file. It is useful for testing the existence and access rights of a file.

The m_attribute is the file attribute. The Microsoft Foundation classes provide an enum type attribute so that you can specify attributes symbolically:

enum Attribute {
   normal =    0x00,
   readOnly =  0x01,
   hidden =    0x02,
   system =    0x04,
   volume =    0x08,
   directory = 0x10,
   archive =   0x20

What you need to do is modify the attributes of the file, then open for writing (AND reset back to original afterwards).  If I remember correctly (from something I did years ago) for system files you would need to remove the system and the hidden attributes in different steps.  The order I can't remember.

Author Comment

ID: 12588972
Yes!  I had the same thought when I woke up this morning - and this does work. I didn't have a problem changing the permissions in any sequence.  The code that works is simply...

       fileAttributes = GetFileAttributes(boot_file);
       SetFileAttributes(boot_file, FILE_ATTRIBUTE_NORMAL);
       ofstream boot_out(boot_file);

                ... modify file

       SetFileAttributes(boot_file, fileAttributes);

Thanks for the help!   I released the point to you.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question