Array of Structures

This is a discussion on Array of Structures within the C Programming forums, part of the General Programming Boards category; Hello, i have a little bit of problem here. My task is to find information about seminar by class (math, ...

1. 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");
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++)
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. You cannot compare stings with == in C use strcmp().

3. 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. 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

5. SOLVED!
strcmp worked! Thank you!

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