reading and writing more than just chars to fstreams

This is a discussion on reading and writing more than just chars to fstreams within the C++ Programming forums, part of the General Programming Boards category; This is a common question for a noob I guess, but I don't seem to be able to find the ...

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    reading and writing more than just chars to fstreams

    This is a common question for a noob I guess, but I don't seem to be able to find the answer anywhere.

    The question is:

    I wish to write a fixed sequence of ints, floats, strings etc. etc. to file and then read them back; what is the most effecient method?

    I used to do something like:

    Code:
       ofstream file("file.fil", ios::out | ios::binary | ios::ate);
       int i = 5;
       float f = 5.6;
       string s = "Dogs like cats";
    
       file.write(&i, sizeof(int));
       file.write(&f, sizeof(f));
       file.write(&s, sizeof(string));
    
       file.close();
    And it worked.

    When returning to my code in dev-cpp however, that use of the write function was not supported, indeed it only showed me one use of the write() function:

    Code:
    write(const char *, unsigned int);
    (or something very close).

    No problem thought I, I simply use:

    Code:
       ofstream file("file.fil", ios::out | ios::binary | ios::ate);
       int i = 5;
       float f = 5.6;
       string s = "Dogs like cats";
    
       file.write(reinterpret_cast<const char *>(&i), sizeof(int));
       file.write(reinterpret_cast<const char *>(&f), sizeof(f));
       file.write(reinterpret_cast<const char *>(&s), sizeof(string));
    
       file.close();
    And yes, it works fine.

    My question is however:
    -------------------------------
    Is there a better or more correct way to do this? Is this method ok? (I have neverseen the reinterpret_cast method I have used documented anywhere)

    Thanks in anticipation

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    string s = "Dogs like cats";
    
    ...
    
    file.write(&s, sizeof(string));
    That is wrong. The size of a string object is fixed (as reported by the sizeof operator), regardless of whether you are storing 10 characters or 100 characters it will always be the same. The internal workings of the object keep track of the characters belonging to the string using dynamic memory allocation.

    If you wanted to write and read a string object to a file you would have to treat it as a character array and since these are variable length objects you would first need to write the length to the file followed by the appropriate number of characters. On reading the data from the file you would first read the size/length of the characters that follow, then dynamically allocate a buffer of an appropriate size and read the characters into this buffer. You could then convert the data in that buffer back into a string object.

    Your use of reinterpret_cast for the float and int variables is correct.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by DominicTrix
    My question is however:
    -------------------------------
    Is there a better or more correct way to do this? Is this method ok? (I have neverseen the reinterpret_cast method I have used documented anywhere)

    Thanks in anticipation

    dt
    you could always take the easy way out:
    Code:
    #include<fstream>
    
    int main()
    {
            double pi=3.14159;
            int i=32000;
            char*hi="hello";
            std::ofstream ofile("test",std::ios::trunc);
    
            ofile<<pi<<i<<hi;
            return 0;
    }
    and you end up with:
    Code:
    3.14159
    32000
    hello
    and reading it in is pretty much the same way...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Sry for no reply, been busy. Thanks guys
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 02:14 AM
  2. reading and writing unsigned chars from a file
    By yahn in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 08:42 PM
  3. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 03:29 PM
  4. Reading from and writing to same file
    By Strait in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2005, 03:37 PM
  5. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21