Thread: fstream driving me carzy

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    Red face 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
    ---wes---

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    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
    Code:
    int num = 10;
    file<< num <<endl;
    You can read it back the same way
    Code:
    int num;
    file>> num;
    *Cela*

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    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?
    Last edited by wesdgreat; 01-26-2003 at 02:44 PM.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    std::ifstream fin;
    fin.open("C:\\testfile.bin", std::ios::binary);
    fin.read(first10bytes, 10);
    fin.close();
    Last edited by FillYourBrain; 01-26-2003 at 02:45 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  2. using fstream
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 04:53 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM