1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
| #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1024
typedef struct STUDENT
{
char name[30];
char age[30];
char sex[30];
char kurs[30];
} STUDENT;
typedef struct ELT
{
STUDENT student;
struct ELT *next;
} ELT;
// чтение файла с базой данных
int ReadFile(char *fname)
{
FILE *f;
char s[1024], *t;
if ((f = fopen(fname, "r")) == NULL)
return 1;
while (fgets(s, N, f) != NULL)
{
for(t = strtok(s, " ~\n\t"); t != NULL; t = strtok(NULL, " ~\n\t"))
printf("%s ", t);
putchar('\n');
}
fclose(f);
return 0;
}
/* добавление элемента в конец очереди */
void Add(ELT **first, ELT **last, STUDENT *student)
{
ELT *q;
q = (ELT *)malloc(sizeof(ELT));
q->student = *student;
q->next = NULL;
if (*last == NULL)
*first = q;
else (*last)->next = q;
*last = q;
}
// создание очереди
int CreateQueue(char *fname, ELT **first, ELT **last)
{
FILE *f;
char s[1024], *t;
STUDENT student;
if ((f = fopen(fname, "r")) == NULL)
return 1;
while (fgets(s, N, f) != NULL)
{
t = strtok(s, " ~\n\t"); strcpy(student.name, t);
t = strtok(NULL, " ~\n\t"); strcpy(student.age, t);
t = strtok(NULL, " ~\n\t"); strcpy(student.sex, t);
t = strtok(NULL, " ~\n\t"); strcpy(student.kurs, t);
Add(first, last, &student);
}
fclose(f);
return 0;
}
// вывод элементов очереди на экран
void PrintQueue(ELT *first)
{
ELT *q = first;
while(q != NULL)
{
printf("%s %s %s %s\n", q->student.name, q->student.age, q->student.sex, q->student.kurs);
q = q->next;
}
}
int main()
{
char fname[] = "base.txt";
ELT *first = NULL, *last = NULL;
//ReadFile(fname);
CreateQueue(fname, &first, &last);
PrintQueue(first);
return 0;
} |