file handling with fstream

with fstream why do seekg and seekp work in unison..fr instance if i bring seekp to the beginning tellg also returns 0 etc...why cant i point to a place in a file to read and another to write at the same time?..(using gcc/g++ under ubuntu)
You do realise that this will overwrite the data after that point you designate and not insert it right?
You do realise that this will overwrite the data after that point you designate and not insert it right?


hehe good catch...yes! i's gonna ask that next once this got sorted out..so can u tell me the way to circumvent both the issues?
The way I like to handle text files is to open them, read it ALL into Memory (vectors are great for this task, see the "insert" member function) and manipulate it that way. Trust me with most of what you'll want to do with text, this is over all the easiest solution.

You'll want to keep these links open while you work on this:

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/iostream/ifstream/
http://www.cplusplus.com/reference/iostream/ofstream/

right...thank u!!

oh..just one more thing..if my file is above the size my RAM can handle, will the OS interact with the vector to move a part of it (not yet accessed) to the disk/swap-area or it's likely the system's gonna crash
Last edited on
You can mess with vram, however it's probably a good idea to stream, rather than whole file copy. Though the latter is much easier. Like Computergeek said, if you're just messing with text, there's no reason for streaming.

You can also split it up into manageable chunks, and interface with the vector of the chunk.

edit: removed an unnecessary however.
Last edited on
You can also split it up into manageable chunks, and interface with the vector of the chunk.


hmm...seems i'v to do the dirty work i thought the system would...
You are using c++ welcome to the jungle =)
Don't think of it as you "having to do the ground work" think of it as being able to control every aspect of the process. This language is great about allowing you to control the little details.
i'v think i'v gotten it better managed with streams...fstream itself...just need to keep track of tellg and tellp in two separate variables and use them with seekg and seekp depending on whether i want to read or write...

but this is plain stupid from c++ (it better have a valid reason to argue)..giving two stuffs (seekg,seekp) that have different usages but dont allow us to implement it that way...plus the inflexibility of overwriting instead of insertion (posts 2&3 in this thread)....frustating
Insertion is not that difficult to implement with existing functions, not very useful in general and is also problematic because you'd have to re-check where stuff is if you keep inserting things all the time.

Also, while the behaviour of seekp and seekg is a bit weird, there is no particular reason to try to read and write something at the same time anyways.
there is no particular reason to try to read and write something at the same time anyways.

:)
my friend...there's no beginning & no end to reasons to do "anything"...its all governed by needs...science (and computer languages including c/c++) wouldn't have evolved this far if we were tied down to that sort of school of thought !!
Last edited on
I'd always rather write a function that dumps some structure into a file in some format and another one that reads from a file and fills in the structure. The format is an implementation detail that is best abstracted away whenever possible.

Of course, if all I want is to edit some text files (for instance, if I'm changing the format for a type of file), I'd write a quick Perl or Python script and get done with it.
Last edited on
Topic archived. No new replies allowed.