struct, enum and binary file read in

This is a discussion on struct, enum and binary file read in within the C Programming forums, part of the General Programming Boards category; Hello eveyone, I have been tasked with writing a server side program. On "my" side I am to open a ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    177

    struct, enum and binary file read in

    Hello eveyone, I have been tasked with writing a server side program. On "my" side I am to open a binary file and fill structs and enum. Here is what I was given as a .h file:
    Code:
    /*classes.h*/
    typedef enum {MW, TR} days;
    
    typedef struct {
         int hour, min;
    } Time;
    
    typedef struct {
         char Dept[5];
         int course, sect;
         days meet_days;
         Time start, end;
         char instr[20]
    } sched_record;
    Here is my problem, I have worked with structs and know how to load a "single" struct with a binary file. How would I load this struct plus the enum? I am not asking for code here. I will probably be back later on with that and the code I have written. I am trying to get the logic part down. Any help would be appreciated. Thanks!

    Sorry about that, I forgot to add what is in the binary file:
    Code:
    Math 102 10 M 0800 0850 Schulte
    Eng  033  1 T 0930 1050 Shakespeare
    Art  308  2 M 0800 1150 VanGogh
    Anth 055 13 T 1200 1325 Kroeber
    CS   125  2 T 0800 0850 Hoare
    Eng  202 10 M 1000 1050 Chaucer
    Chem 100  5 T 1100 1250 Pauling
    Phys 395  2 T 1200 1250 Einstein
    CS   125  4 M 1030 1120 Knuth
    Math 420  2 T 0800 0950 al-Khowarizmi
    Last edited by csharp100; 03-28-2012 at 09:21 PM. Reason: added binary file

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > days meet_days;
    You don't need to do anything extra (from what you already know).

    The value stored in meet_days will be read/written just fine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If what you posted is the actual contents of the file then it's not a binary file but a column-structured text file. So, for example, you won't want to use fread to read it. fscanf could pick it apart nicely. When you read the days character just translate it to the proper enum value. The times can be picked apart like so:

    fscanf(fp, "%2d%2d", &hours, &mins);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Quote Originally Posted by oogabooga View Post
    If what you posted is the actual contents of the file then it's not a binary file but a column-structured text file. So, for example, you won't want to use fread to read it. fscanf could pick it apart nicely. When you read the days character just translate it to the proper enum value. The times can be picked apart like so:

    fscanf(fp, "%2d%2d", &hours, &mins);
    I agree, our professor had a text and binary file. We are to use the binary file. The text was for us to know what was in the binary file.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Quote Originally Posted by Salem View Post
    > days meet_days;
    You don't need to do anything extra (from what you already know).

    The value stored in meet_days will be read/written just fine.
    Sweet! I had a feeling about that but wanted to make sure before I started writing code. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from a struct to a binary file, to a struct again
    By starshiptrooper in forum C++ Programming
    Replies: 4
    Last Post: 10-07-2011, 09:37 AM
  2. Binary read from file
    By dereach in forum C Programming
    Replies: 5
    Last Post: 02-27-2008, 02:34 PM
  3. Read binary file
    By Ken JS in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2007, 11:12 AM
  4. Read a binary file
    By Sue Paterniti in forum C Programming
    Replies: 8
    Last Post: 04-29-2002, 02:36 AM

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