Structures, fread(), sizeof()

This is a discussion on Structures, fread(), sizeof() within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am currently revising for a paper based C++ examination, which is tomorrow. I've come across a question ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    Structures, fread(), sizeof()

    Hello everyone,

    I am currently revising for a paper based C++ examination, which is tomorrow. I've come across a question which asks to write a program that will read data into a structure from an external file 'using a single statement'.

    Would i be able to use fread() passing in the pointer to the structure, the size of the entire structure using sizeof() and passing in the pointer of the file to be read from - all in one statement.

    or would i have to iterate through each element of the structure separately, the structure contains elements of different sizes in memory, eg. int - 4bytes and long - 8 bytes.

    Any input would be great thanks

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    fread() takes a void pointer. So the procedure to do this would be to allocate enough space and then call fread() once.
    After you read the data you can cast the structure over it and get the values inside the struct.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    the structure has already been constructed, so can i not just read the data in and point it to the already created(but empty) structure, with say 6 members? making sure i don't overflow using sizeof(my structure).

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Yes, if its preallocated, this should work, too.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    This is discouraged practice. You should overload a stream operator (>> or <<) for your struct, in which you write and read your members individually.
    Then you can simply do

    mystruct s;
    file >> s;

    This will ensure portability, as well as avoiding (possible) undefined behavior (especially with classes).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Replies: 6
    Last Post: 10-15-2007, 09:05 AM
  4. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 12:00 PM
  5. Replies: 5
    Last Post: 04-11-2002, 12:29 PM

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