#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
typedef struct{
char dummy[256];
char surname[225];
char name[256];
int ID;
char house[256];
char street[256];
char town[256];
char county[256];
char area[256];
char country[256];
char postcode[256];
int phone;
char email[256];
char boattype[256];
char boatname[256];
}user;
FILE *file;
int i=0;
char number_of_people[256];
int nop;
char yetdummy[256];
char yetdummy2[256];
file = fopen("members.data","r");
if(file==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
printf("File opened successfully.\n");
}
fscanf(file,"%s \n", number_of_people);
//printf("%s \n", number_of_people);
nop = atoi(number_of_people);
//printf("%d\n", nop);
user usera[nop];
for (i=0; i<nop; i++){
fgets(usera[i].dummy, 256, file);
fgets(usera[i].surname, 256, file);
fgets(usera[i].name, 256, file);
fgets(yetdummy, 256, file);
usera[i].ID = atoi(yetdummy);
fgets(usera[i].house, 256, file);
fgets(usera[i].street, 256, file);
fgets(usera[i].town, 256, file);
fgets(usera[i].county, 256, file);
fgets(usera[i].area, 256, file);
fgets(usera[i].country, 256, file);
fgets(usera[i].postcode, 256, file);
fgets(yetdummy2, 256, file);
usera[i].phone = atoi(yetdummy2);
fgets(usera[i].email, 256, file);
fgets(usera[i].boattype, 256, file);
fgets(usera[i].boatname, 256, file);
printf("Name is %s\n", usera[i].name);
printf("ID is %d\n", usera[i].ID);
}
fclose(file);
}
This is my code so far. I got it to read members from file and it works fine and reads them all in an array of structures. You can probably see how stupid I am in programming if you look at my code)))
What can I do next? This is the sample code I was talking about earlier, I think it is very similar to what I am looking for, but I can not see how the student data is fed into the program or how the key variable is defined...
#include<stdlib.h>
#include<stdio.h>
typedef struct student_data STUDENT_DATA;
struct student_data {
int student_ID;
int student_grade;
STUDENT_DATA *left, *right;
};
STUDENT_DATA *new_student, *cur_student;
if ((new_student = malloc(sizeof(STUDENT_DATA))) == NULL) { abort(); }
new_student->student_ID = newID;
new_student->student_size = newsize;
new_student->left = NULL;
new_student->right = NULL;
if (!students) { students = new_student; return; }
cur_student = students;
while (cur_student) {
if (newID == cur_student->student_ID) { abort(); }
if (newID < cur_student->student_ID) {
if (cur_student->left == NULL) {
cur_student->left = newstudent;
return 1;
}
cur_student = cur_student->left;
} else {
if (cur_student->right == NULL) {
cur_student->right = newstudent;
return 1;
}
cur_student = cur_student->right;
}
}
Thanks a lot!
Oh, and by the way I use Dev-C++