fstream.write()

This is a discussion on fstream.write() within the C++ Programming forums, part of the General Programming Boards category; im currently studying how to manipulate files using seekp, seekg, etc, and i am attempting to use the command fstream.write. ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    fstream.write()

    im currently studying how to manipulate files using seekp, seekg, etc, and i am attempting to use the command fstream.write. heres the snippet of code i've currently got:

    Code:
    ....
      fstream fileObject("data.dat",ios::out);
    ....
    
      cout << "enter id: ";
      cin >> temp->ID;
      cout << "enter name: ";
      cin >> temp->name;
      cout << "enter birthdate: ";
      cin >> temp->birthdate;
      cout << "enter position: ";
      cin >> temp->position;
    
      fileObject.write(reinterpret_cast<const char *>(temp),sizeof(temp));
    and heres what i get in the data.dat file:

    Code:
       
    i have a feeling its got something to do with the reinterpret_cast<const char*> bit, and i was hoping someone could please give me any insight into this peculiar problem. thanks a lot in advance! (borland 5.02)

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    is temp a struct or a class? you shouldn't do that sort of thing with a class. A struct might be fine.

    aside from that, you are writing this stuff out as binary so the name is the only thing that might be readable to you (that's if it isn't dynamic memory).
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    temp is a class. whats wrong with using a class over a struct? thanks.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Well, I suppose it's not entirely that way. struct can be just as bad. The problem comes when your data location is unsure. I would prefer to make a call like this:

    temp->Save(fileobject);

    or something. This would allow an object like temp to control how it saves.

    if you used a std::string for "name" it will not work cause you'll get memory addresses instead of string data. Things like that are why you shouldn't just write out a class that way.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Can't you just do something like:

    Code:
    fileObject << temp->ID << endl
               << temp->name << endl
               << temp->birthdate << endl
               << temp->position << endl;
    "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

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    i could, but i want to figure out how using the write() command

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    write() is just bytes. You have to make sure you have all of the bytes you really want to save is all.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Shadow12345
    Guest
    This is irrelavent to anything, but
    fstream fileObject("data.dat",ios: out);
    could be written as
    ofstream fout;
    just declares an instance of output file stream.
    and then you just call
    fout.open("data.dat")

    I dunno, it isn't important, and your way works fine

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    write() should only be used with character data. If any part of whatever you are planning to output with write() contains binary data, as in any integers/floats or the like, you will need to use some other method. One problem is that binary data written out by this function could possibly be interpretted as an EOF character which would effectively close the file even though you think you are still writing more data to it.
    "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

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    temp is a pointer to a class object but you want to save the data in the class object being pointed to. Therefore I wouldn't try to write() temp, I'd try to write() *temp.

    fileobject.write(reinterpret_cast<char*> *temp, sizeof(*temp));

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's incorrect hk_mp5kpdw. write is what you use for binary specifically.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    elad, I hadn't caught that. dang that sucks. either way, there's no way of knowing what's going on inside a given class, you should never just write out the entire thing as binary.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    that's incorrect hk_mp5kpdw. write is what you use for binary specifically.
    Sorry bout that... brain fart. I was thinking of one thing but typing something else.
    "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

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Actually you're right in a way..

    The problem lies in the reinterpret_cast.

    The class contains binary data, when converted to a char* the wite method prints what the class contains, byte for byte.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

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