If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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?

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.

Re: Difference between ostream and ofstream

Hi,

Originally Posted by Skizmo

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?

Originally Posted by Skizmo

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...

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.

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.

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.

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.

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.

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.

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.