fstream driving me carzy
i'm trying to figure out how to use the fstream library to write and read to/from files... i basically can write to files with not to much trouble, being that it is just like doing a cout... but when it comes to reading from a file i can't do crap......
basically i want to be able to get something more than the very first string out of a file, i want to be able to specify a location and get something from that location
even more i would want to be able to store and recover information other than strings
(ie: int number = 10
then send number to a file
later search for number and find a 10 )
i have been all over the internet trying to find a way to do this and found nothing useful or that i could understand
PLEASE SOMEONE HELP
--thanks in advance
Reading from an fstream in text mode is exactly like reading from cin. Just remember that and you'll know what you can and can't do.
>>i want to be able to specify a location and get something from that location
Unless it's a binary file you can't safely do that. Remember the cin similarities? Can you specify a location in the stream from cin, nope. :-) If you open the file as ios_base::binary then you can use seekg() to go to a certain offset though.
>>even more i would want to be able to store and recover information other than strings
Let's say you write an integer to a file like so
You can read it back the same way
int num = 10;
file<< num <<endl;
ok thanks but now how do i use a binary file?
(i read about it somewhere but all the explanations of going through it were confusing, i'm sure someone here can make more snese out of it for me)
and about how if i put num into a file ( file << num )
will i be able to read it back out even if i change the value of num of add anything else?