Thread: Binary File Structs

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

    Binary File Structs

    Hello everyone, I have the following binary file struct;
    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;
    My question is this, Can someone show me how to access a particular member of that struct. i.e I need to search through the binary file for "course." The whole binary file has been loaded into structs. How can this be done? Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You declare a variable of type sched_record. Let's assume that variable is called x. You use fread to get a struct from the file and put it in x, then you just use the . operator:
    Code:
    x.course
    If you need to search through the file, you just put your fread in a loop.

    Frankly, if you're this clueless, you should read your textbook and class notes, and probably some tutorials on line, for both structs and file IO. Start here: C Tutorial - Learn C - Cprogramming.com.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by anduril462 View Post
    You declare a variable of type sched_record. Let's assume that variable is called x. You use fread to get a struct from the file and put it in x, then you just use the . operator:
    Code:
    x.course
    If you need to search through the file, you just put your fread in a loop.


    Frankly, if you're this clueless, you should read your textbook and class notes, and probably some tutorials on line, for both structs and file IO. Start here: C Tutorial - Learn C - Cprogramming.com.
    Interesting, since I have tried that and all I get is garbage. I believe my question was on SEARCHING for a particular member. I can print out particular members just fine. I understand this works fine for TEXT files, but not BINARY files unless there is some sort of index pointer. That is where I am not understanding.
    Thank you for being quick to judge, also thanks for replying.
    Last edited by csharp100; 04-12-2012 at 11:17 AM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    How you do this obviously depends on how you've written the file which we don't know.

    Programming isn't like that one weird art movement where people essentially vomit paint onto a canvas; throwing crap at your compiler and expecting it to do what you want isn't a thing that will work.

    Soma

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by csharp100 View Post
    I believe my question was on SEARCHING for a particular member....Thank you for being quick to judge, also thanks for replying.
    I wasn't being judgemental, but the only questions you actually posed were:
    Quote Originally Posted by csharp100 View Post
    My question is this, Can someone show me how to access a particular member of that struct.
    and
    Quote Originally Posted by csharp100 View Post
    How can this be done?
    The "How can this be done?" question I assumed referred to the confusion of accessing a struct member (the thing you actually asked for help on). The only code you provided was a struct definition, implying maybe you didn't actually know how to work with structs. I truly believed you were looking for help on simply accessing a member, which is such a fundamental thing for working with structs, that I suggested you work on your foundation before. There was nothing to indicate you needed help with the searching itself, merely a mention that it was the end goal of your program.

    i.e I need to search through the binary file for "course." The whole binary file has been loaded into structs. How can this be done? Thanks!
    Loaded how? Into an array? Into umpteen different individual structs? Into a linked list of structs? More code would be good -- give me all the relevant code if you can, maybe the whole program if it's not too big.

    Quote Originally Posted by csharp100 View Post
    Interesting, since I have tried that and all I get is garbage. I believe my question was on SEARCHING for a particular member. I can print out particular members just fine. I understand this works fine for TEXT files, but not BINARY files unless there is some sort of index pointer. That is where I am not understanding.
    What exactly did you try? It's really hard for us to trouble shoot problems with what you tried if you don't provide us the code for what you tried. You can definitely do what you're asking about with binary files, but you use different methods than with text files, and may need to "massage" the data you read a tiny bit to work with output funcitons like printf.

    As an aside, get used to "rough" speech on forums. It's about information exchange, not ego stroking, so we strive to be clear and concise, which often sounds blunt or rude to people. We volunteer our time because we like to help people, not because we want to be a-holes, but we don't want to feel our free time is being wasted or abused (not that you really were). But we really do appreciate it if the question-askers are as clear and concise as possible too (but not so concise as to omit necessary info). If you haven't yet, read through this page, it will greatly increase your effectiveness in all forums: How To Ask Questions The Smart Way.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Thank you for being quick to judge
    If anduril's answer is not to your liking, may I suggest that your question was not well-phrased.

    Look at this:
    Can someone show me how to access a particular member of that struct
    That seems like a pretty basic question.

    The whole binary file has been loaded into structs.
    So it's not a binary file anymore? It's an array of structs?

    I can print out particular members just fine.
    How can you print out particular members just find but not be able to search for a particular member?

    You'll either have to post code so we can see exactly what you're doing or be more specific in your question.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Either the data is loaded into an array or list of structs memory in which case the mere fact that it came from a file is now irelevant, as it is in memory same as if it had always been in memory, or the data has not been loaded from file into structs in which case you need help with loading the data from file into memory.
    Which is it?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Code:
    int findRecord(char *fileName)
    {
        FILE *file;
        RECORD data;
        int recNumber = 0;
    
        printFile("cat.bin");
        printf("\n");
        printf("Enter record you want to search: ");
        scanf("%d", &recNumber);
    
    
        file = fopen(fileName, "rb");
    
        fseek(file, recNumber * sizeof(RECORD), SEEK_SET);
        fread(&data, sizeof(RECORD), 1, file);
        printHeadings();
        printf("%-26d %-26s %d\n", data.recNumber, data.productName, data.numberAvailable);
    
        return 0;
    }
    If I replace recNumber with say... numberAvailable, (from the aforementioned struct), on the fseek line and rerun the program I get garbage output. Mind you I did change the variable name on lines 5 and 10.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can only do random access with the record number (assuming it is the offset of the record in the file). Obviously using numberAvailable is not going to magically seek to the proper record (and presumably more than one record could have the same numberAvailable anyway). To search by anything other than the recordNumber you'll have to do a linear search.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Sounds good. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary file work using structs and enum
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 03-31-2012, 03:58 PM
  2. help !! : problem with binary file, and fread into structs
    By stevehicks in forum C Programming
    Replies: 5
    Last Post: 02-14-2010, 10: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, 10:52 AM