Difference between ostream and ofstream
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Difference between ostream and ofstream

  1. #1
    Join Date
    Aug 2002
    Posts
    581

    Difference between ostream and ofstream

    Hi, ALL,
    I have a function that's open an ofstream and writes some data to it. It's working fine.

    I also have a function that opens the same file using ostream. It opens it for adding data. The problem is that after creating the file, I'd like to move the file pointer behind the eof. However, it looks like I couldn't do it.

    I'm using Visual Studio 6.

    Is it a bug in the compiler and I should upgrade or it is a problem with my code and I should use ofstream?

    Thank you.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,062

    Re: Difference between ostream and ofstream

    Why you want to move the filepointer to behind eof?
    Do you just want to jump to the end of the file?

  3. #3
    Join Date
    Aug 2002
    Posts
    581

    Re: Difference between ostream and ofstream

    Marc,

    That's how my data will be stored:

    Code:
    file signature
    --------- OBJECT HEADERS --------
    object header 1
       position of object 1
       size of object 1
       name of object 1
    object header 2
       position of object 2
       size of object 2
       name of object 2
    ------- END OF OBJECT HEADERS -------
    .......
    
    object 1 data
    .......
    
    object 2 data
    ......
    When I create a file, I have a 1 object to store, therefore I will create a file signature, the object header 1, and object 1 data.

    Thank you.

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Difference between ostream and ofstream

    Why you want to move the filepointer to behind eof?
    When I create a file, I have a 1 object to store, therefore I will create a file signature, the object header 1, and object 1 data.
    That's not an answer. The layout of the file has nothing to do with the fact that you want to move your filepointer behind eof. What you want is a file that contains for example 100 bytes, and you want to move the filepointer to 110. If you want to do this, simply add 10 dummy bytes and then you can move your filepointer to 110. Later on you can replace the 10 dummy bytes with actual data you want to store.

    I guess the problem you are facing is the fact that you want to store the data and afterwards you want to create a header for that data that needs to be in front of the data. You could try to calculate the size of the header without actually creating the header. If you know the headersize you can write a block of dummy data that later on will be replaced with the actual header. You could also create 2 files. 1 file for the data and 1 endresult file. First you write all the data to file 1, then create the header and write it to file 2. After that you read the data from file 1 and append it at file 2.
    Last edited by Skizmo; February 4th, 2009 at 11:24 AM.

  5. #5
    Join Date
    Aug 2002
    Posts
    581

    Re: Difference between ostream and ofstream

    Hi,
    Quote Originally Posted by Skizmo View Post
    That's not an answer. The layout of the file has nothing to do with the fact that you want to move your filepointer behind eof. What you want is a file that contains for example 100 bytes, and you want to move the filepointer to 110. If you want to do this, simply add 10 dummy bytes and then you can move your filepointer to 110. Later on you can replace the 10 dummy bytes with actual data you want to store.
    Ok, but I have 2 more questions:
    1. How do I extend the file to be 110 bytes? Do I just write additional 10 '\0' symbols?
    2. If I later write to those 10 bytes, it will not increase the size of the file, right?
    Quote Originally Posted by Skizmo View Post
    I guess the problem you are facing is the fact that you want to store the data and afterwards you want to create a header for that data that needs to be in front of the data. You could try to calculate the size of the header without actually creating the header. If you know the headersize you can write a block of dummy data that later on will be replaced with the actual header. You could also create 2 files. 1 file for the data and 1 endresult file. First you write all the data to file 1, then create the header and write it to file 2. After that you read the data from file 1 and append it at file 2.
    No, it's actually the other way around. I'm storing the header, and then the data.
    But when I store the header using ofstream, I can seek behind EOF, and that's where the confusion started...

    Thank you.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Difference between ostream and ofstream

    Quote Originally Posted by OneEyeMan View Post
    Hi,

    Ok, but I have 2 more questions:
    1. How do I extend the file to be 110 bytes? Do I just write additional 10 '\0' symbols?
    Write whatever you want, so long as it's 10 bytes long. Use unformatted output for this of course.

    2. If I later write to those 10 bytes, it will not increase the size of the file, right?
    The only time the file size will change is if you write past the current end of the file. Otherwise you'll just overwrite whatever is currently at the file pointer's position.

    But when I store the header using ofstream, I can seek behind EOF, and that's where the confusion started...
    http://www.cplusplus.com/reference/i...eam/seekp.html

    Is that what you're using? That documentation doesn't say anything about what happens if you seek past EOF, so it's entirely possible that it will expand the file size and zero-pad the intervening bytes if you do that. I think there is a function which is explicitly documented to do that somewhere, but I can't find it for now.

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Difference between ostream and ofstream

    But when I store the header using ofstream, I can seek behind EOF, and that's where the confusion started...
    Don't be confused by what you can do, be confused by what you can't do

    I have have never used ofstream so I don't know why you can write beyond it's actual size, but I do know that with a 'normal' file you can't because there is nothing to go beyond. They only thing I can 'guess' is that if you seek beyond eof and you start writing, ofstream automatically fills it with '\0' chars. To be sure, simply try it and see what happens.

    1. How do I extend the file to be 110 bytes? Do I just write additional 10 '\0' symbols?
    yes.

    2. If I later write to those 10 bytes, it will not increase the size of the file, right?
    As soon as you write the '\0' from question 1, the file size will increase. If you later on overwrite those 10 bytes with other data, the size will not increase.

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,062

    Re: Difference between ostream and ofstream

    Since you posted this in the visual C++ forum, I'm assuming you are Windows.
    In Windows it seems you can use SetFilePointer(Ex) to let it point behind the EOF.
    Quote Originally Posted by MSDN
    It is not an error to set a file pointer to a position beyond the end of the file. The size of the file does not increase until you call the SetEndOfFile, WriteFile, or WriteFileEx function. A write operation increases the size of the file to the file pointer position plus the size of the buffer written, which results in the intervening bytes uninitialized.
    See SetFilePointer.

    I didn't know this, so we all learned something new

    Of course this means you should use the Windows way of opening files instead of ofstream.
    So, if you want to stay platform independent, just to as the previous replies say and write some dummy date.

  9. #9
    Join Date
    Nov 2003
    Posts
    1,800

    Re: Difference between ostream and ofstream

    Seeking past the end of the file is defined by Posix:
    The fseek() function shall allow the file-position indicator to be set beyond the end of existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap.
    This just happens to be what Windows does as well. seekp() is implemented in terms of fseek() so it works for C++ files streams as well.

    gg

  10. #10
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,062

    Re: Difference between ostream and ofstream

    Quote Originally Posted by Codeplug View Post
    Seeking past the end of the file is defined by Posix:
    This just happens to be what Windows does as well. seekp() is implemented in terms of fseek() so it works for C++ files streams as well.

    gg
    Cool, didn't know that

  11. #11
    Join Date
    Aug 2002
    Posts
    581

    Re: Difference between ostream and ofstream

    Quote Originally Posted by Codeplug View Post
    Seeking past the end of the file is defined by Posix:
    This just happens to be what Windows does as well. seekp() is implemented in terms of fseek() so it works for C++ files streams as well.

    gg
    It looks like this behavior is available only for ofstream and not ostream...

    Thank you.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Difference between ostream and ofstream

    ostream is a base class of ofstream, I'm pretty sure. There's an IS-A relationship. Thus so long as the function is available to be called and the underlying object is an ofstream, the behavior will be the same.

    What behavior you'd expect for an ostream that is *not* an ofstream is an open question, but probably not one you'd need to worry about too much.

  13. #13
    Join Date
    Aug 2002
    Posts
    581

    Re: Difference between ostream and ofstream

    Lindley,
    I was just thinking that maybe it is a correct behavior, since ostream is not necessary a ofstream. It can be char*. And writing something behind EOF, might be undefined.

    And yes, they are parent-child. But the behavior I see here contradict what you said.

    So I guess I have only one choice - work with ofstream all the way through... ;-)

    Thank you.

  14. #14
    Join Date
    Aug 2002
    Posts
    581

    Re: Difference between ostream and ofstream

    Hi, ALL,
    Just to follow-up, can I do a casting from parent to child, i.e. from ostream to ofstream?

    This way I will be talking to the stream in terms of ofstream, and hopefully will resolve my issue...

    Thank you.

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Difference between ostream and ofstream

    The only time casting from parent to child (using dynamic_cast) actually gets you anything is when you need to access a function which doesn't exist in the parent's public interface.

    If the functions are all present in the parent, and you know for certain that the parent is actually an instance of the child, then you can be certain the behavior of each function will be identical to if you called the child's function directly.

    Base classes are merely an abstraction allowing you to handle multiple types of objects through a common interface. They don't (usually) have behavior distinct from the child they're abstracting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center