Thread: Loading a struct with data from a file

  1. #1
    Registered User
    Join Date
    Oct 2001

    Question Loading a struct with data from a file

    How do you load a structure with data from a file?

    I am running Windows 98 with Microsoft Visual C++ 6.0.

    The code attached has errors in it from undeclared identifiers to subscript requires an array or pointer type.

    I am new to C, an what appeared to be an easy problem can turn really nasty in a second.

    Please help,

    The name of the file I was trying to load from was inventry.dat

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    structures should be written to binary files not text files

    you can read a structure from a binary file with

    fread(struct, sizeof(structure), 1, file);

    this will read the whole structure into the structure pointed to by struct

    if you changed the 1 to a 2 it would 2 structures, then struct would have to be a pointer to an array of structures.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM