Writing EOF into an output stream

Posted on 2004-11-12
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
    LVL 5

    Expert Comment

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

    Good Luck!

    LVL 43

    Expert Comment

    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

    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 43

    Accepted Solution

    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

    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

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    List out all word 7 192
    string initialization in java 11 94
    canBalance challenge 34 57
    mapBully challenge 6 53
    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    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.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now