binary file work using structs and enum

This is a discussion on binary file work using structs and enum within the C Programming forums, part of the General Programming Boards category; Hello everyone, I get a "Segmentation Fault" when I try to run this program. I think my problem is with ...

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

    binary file work using structs and enum

    Hello everyone,
    I get a "Segmentation Fault" when I try to run this program. I think my problem is with the structs and enum. Specifially the enum but I could be wrong. I have enclosed my classes.h file and loadFile.c file.
    Code:
    #ifndef CLASSES_H_INCLUDED
    #define CLASSES_H_INCLUDED
    
    #include <stdio.h>
    #include <stdlib.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;
    
    
    #endif // CLASSES_H_INCLUDED
    Code:
    #include "classes.h"
    
    void main()
    {
        FILE *filePointer;
        sched_record data;
    
        filePointer = fopen ("classes.db", "rb");
        if (filePointer == NULL)
        {
            puts("Cant open file");
            exit(1);
        }
        while (!feof(filePointer))
        {
            if(fread(&data, sizeof(sched_record), 1, filePointer)!=0)
            {
                printf("\n%s %d %d %s %d %d %s", data.Dept, data.course, data.sect, data.meet_days, data.start, data.end, data.instr);
                fclose(filePointer);
            }
        }
    }
    The file we are to use is a binary file and it is called classes.db. Now we have a text representation and it is as follows:
    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
    Does anyone know how I might correct this? I appreciate any help.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Here's what I get when I compile your code:
    Code:
    $ make sched
    gcc -Wall -g -std=c99  -lm -lpthread -lcurses -lefence  sched.c   -o sched
    sched.c:18: warning: return type of ‘main’ is not ‘int’
    sched.c: In function ‘main’:
    sched.c:33: warning: format ‘%s’ expects type ‘char *’, but argument 5 has type ‘unsigned int’
    sched.c:33: warning: format ‘%d’ expects type ‘int’, but argument 6 has type ‘Time’
    sched.c:33: warning: format ‘%d’ expects type ‘int’, but argument 7 has type ‘Time’




    That should help a bit.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Thank you very much! Just need to change the enum. Looking that up now.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Ok, got it working.
    Code:
    #ifndef CLASSES_H_INCLUDED
    #define CLASSES_H_INCLUDED
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BINFILE "classes.bin"
    
    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;
    
    
    #endif // CLASSES_H_INCLUDED
    Code:
    #include "classes.h"
    
    int main(void)
    {
        FILE *filePointer;
        sched_record data;
    
        filePointer = fopen (BINFILE, "rb");
        if (filePointer == NULL)
        {
            puts("Cant open file");
            exit(1);
        }
        while (!feof(filePointer))
        {
            if(fread(&data, sizeof(sched_record), 1, filePointer)!=0)
            {
                printf("\n%s %d %d %2s %02d%02d ", data.Dept, data.course, data.sect, data.meet_days== MW ? "MW" : "TR", data.start.hour, data.start.min);
                printf("%02d%02d %s", data.end.hour, data.end.min, data.instr);
            }
        }
    
        fclose(filePointer);
    
        return 0;
    }

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Normally you would join these two lines
    Code:
        while (!feof(filePointer))
        {
            if(fread(&data, sizeof(sched_record), 1, filePointer)!=0)
    like this, getting rid of the unnecessary (and incorrect, actually) feof()
    Code:
        while (fread(&data, sizeof data, 1, filePointer) == 1)
    Note that it's best to compare to the count parameter (not 0) since if anything else is returned, something went "wrong" (presumably end of file, but if it was some other error you'd still want to stop the loop although feof() would not be true!). Also you can use sizeof data which is a little slicker since it doesn't have to be changed if data's data type changes (actually, if data becomes a pointer, then you'd have to use sizeof *data).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Thanks! I appreciate that. Updated code
    Code:
    #include "classes.h"
    
    int main(void)
    {
        FILE *filePointer;
        sched_record data;
    
        filePointer = fopen (BINFILE, "rb");
        if (filePointer == NULL)
        {
            puts("Cant open file");
            exit(1);
        }
        while (fread(&data, sizeof data, 1, filePointer) == 1)
        {
                printf("\n%s %d %d %2s %02d%02d ", data.Dept, data.course, data.sect, data.meet_days== MW ? "MW" : "TR", data.start.hour, data.start.min);
                printf("%02d%02d %s", data.end.hour, data.end.min, data.instr);
        }
    
        fclose(filePointer);
    
        return 0;
    }
    Last edited by csharp100; 03-31-2012 at 04:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct, enum and binary file read in
    By csharp100 in forum C Programming
    Replies: 4
    Last Post: 03-29-2012, 05:43 AM
  2. help !! : problem with binary file, and fread into structs
    By stevehicks in forum C Programming
    Replies: 5
    Last Post: 02-14-2010, 09:29 AM
  3. Saving Struct to Binary File/Copying Structs
    By timmeh in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2009, 08:44 PM
  4. Read a file (binary) into an array of structs
    By Separ in forum C Programming
    Replies: 3
    Last Post: 04-14-2009, 09:09 PM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 09:52 AM

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