Thread: Array of Structures

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Array of Structures

    Hello, i have a little bit of problem here.
    My task is to find information about seminar by class (math, physics, you name it). my question is how to make programm go trough specific part of structure in my example seminar[i].class? and search for a match and print out the info that is in this classes seminar?
    the code i got so far:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <windows.h>
    #define N 15
    int main()
    {
        FILE *struct_file;
        struct seminars
        {
               int Nr;
               char class[30], professor[30], group_no[30], day[30], duration[30], auditory[30];
               } seminar[N];
        int i,j,t;
        system("cls");
        printf("Please fill info about seminars\n");
        for (i=0; i<N; i++)
        {
            seminar[i].Nr=i+1;
            printf("\nFill the info about %d. seminar\n", i+1);
            printf("Class: ");
            gets(seminar[i].class);
            printf("Professor: ");
            gets(seminar[i].professor);
            printf("Group number: ");
            gets(seminar[i].group_nr);
            printf("Day: ");
            gets(seminar[i].day);
            printf("Duration of the class: ");
            gets(seminar[i].duration);
            printf("Auditory: ");
            gets(seminar[i].auditory);
            }
        struct_file=fopen("seminars.txt","w");
        for(i=0; i<N; i++)
                 fwrite(&seminar, sizeof(seminar),1, struct_file);
        fclose(struct_file);
        struct_file=fopen("seminars.txt","r");
        for(i=0; i<N; i++)
                 fread(&seminar, sizeof(seminar),1, struct_file);
        fclose(struct_file);
        system("cls");
        printf("List of seminars: \n");
        for(i=0; i<N; i++)
                 printf(" %d %s %s %s %s %s %s\n", seminars[i].Nr, seminar[i].class,
                 seminar[i].professor, seminar[i].group_no, seminar[i].day, 
                 seminar[i].duration, seminar[i].auditory);
        char classneed[30];
        printf("What kind of class are you looking for: \n");
        gets(classneed);
        struct_file=fopen("seminars.txt", "r");
        for (i=0; i<N; i++)
        {
            if (seminar[i].class==classneed) //this is the part where trouble start.
            printf("This class is studied at %d. seminar", i);
            }
        fclose(struct_file);
        getch();
        return 0;
            }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    You cannot compare stings with == in C use strcmp().

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also:
    • Don't use gets. Read this: Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows. Use fgets instead (but remember to trim the newline).
    • You should check the return value of fopen to make sure it's not NULL before you do any file operations. If you don't, and fopen fails, you may get a seg fault.
    • You should check the return values of fwrite and fread to make sure they worked before relying on the data. Failure to do so could result in undefined behavior.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, but what is your question?

    > fwrite(&seminar, sizeof(seminar),1, struct_file);
    This writes the whole array in one step, there's no need to loop here.

    Use (or abuse) of gets() is discussed here
    SourceForge.net: cpwiki
    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.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    SOLVED!
    strcmp worked! Thank you!

    I needed to find number of the seminar comparing classes if they match then print out info about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of Structures
    By twistedtraceur in forum C Programming
    Replies: 3
    Last Post: 02-21-2011, 11:50 AM
  2. array of pointers to an array of structures
    By phoneix_hallows in forum C Programming
    Replies: 3
    Last Post: 08-27-2009, 11:13 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. 2 Dimenstional Array vs Array of Structures
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2004, 04:51 AM
  5. Array of Structures
    By TeenyTig in forum C Programming
    Replies: 1
    Last Post: 02-17-2002, 09:34 PM