Thread: Files

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367

    Files

    Hi, i'm after reading a block of data from a file which is in binary, and copying the data to a structure. The struture is exactly alligned to match the data within the file. Could anyone advise us on the type of function to use.... Thanks.
    Be a leader and not a follower.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Since it's C++ and it's a binary file: ifstream::read()

    Code:
    STRUCTURE MyStructure;
    
    ifstream ReadFile;
    ReadFile.open("MyFile.dat", ios::in | ios::binary);
    
    if(!ReadFile.fail())
    {
       ReadFile.read((char*)&MyStructure, sizeof(STRUCTURE));
       ReadFile.close();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    right that looks great thanks, why the typecasting to a char pointer though?
    Be a leader and not a follower.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by subdene
    right that looks great thanks, why the typecasting to a char pointer though?
    Since the ifstream class have no idea what datatypes you may have created, the only type it supports to read/write are char. Probably beacuse one char is 1 byte (most of the time at least).
    Otherwise you had to overload the read function yourself for every datatype you have made.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    right ok thanks.
    Be a leader and not a follower.

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Magos, i've implemented the code, but all i get in the structure is garbage.... any ideas? Binary file is ok.... can't see what it could be.
    Be a leader and not a follower.

  7. #7
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Post some code.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  8. #8
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    all right got it sorted now thanks, i was reading too far into the binary file.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM