-
February 3rd, 2009, 08:29 PM
#1
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.
-
February 4th, 2009, 03:08 AM
#2
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?
-
February 4th, 2009, 12:09 PM
#3
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.
-
February 4th, 2009, 12:20 PM
#4
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 12:24 PM.
-
February 4th, 2009, 01:50 PM
#5
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...
Thank you.
-
February 4th, 2009, 01:59 PM
#6
Re: Difference between ostream and ofstream
Originally Posted by OneEyeMan
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.
-
February 4th, 2009, 02:02 PM
#7
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.
-
February 4th, 2009, 02:12 PM
#8
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.
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.
-
February 4th, 2009, 02:34 PM
#9
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
-
February 4th, 2009, 02:40 PM
#10
Re: Difference between ostream and ofstream
Originally Posted by Codeplug
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
-
February 4th, 2009, 03:05 PM
#11
Re: Difference between ostream and ofstream
Originally Posted by Codeplug
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.
-
February 4th, 2009, 03:13 PM
#12
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.
-
February 4th, 2009, 03:23 PM
#13
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.
-
February 6th, 2009, 06:44 PM
#14
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.
-
February 6th, 2009, 08:26 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|