Loading a struct with data from a file

This is a discussion on Loading a struct with data from a file within the C Programming forums, part of the General Programming Boards category; How do you load a structure with data from a file? I am running Windows 98 with Microsoft Visual C++ ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    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
    Attached Files Attached Files
    • File Type: c test.c (595 Bytes, 139 views)

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    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, 03:16 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02: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, 11:36 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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