-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_data.c
More file actions
178 lines (144 loc) · 5.23 KB
/
student_data.c
File metadata and controls
178 lines (144 loc) · 5.23 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <stdlib.h>
typedef struct student_data {
char name[26];
char id[5];
char letter_grade;
int exams[3];
int hws[3];
} STUDENT_DATA;
typedef enum bool {
FALSE,
TRUE
} BOOL;
// Fucntion Declarations
BOOL read_from_text_file(FILE *text_file, STUDENT_DATA *a_student);
BOOL write_into_binary_file(FILE *binary_file, STUDENT_DATA *a_student);
BOOL read_from_binary_file(FILE *binary_file, STUDENT_DATA *a_student);
BOOL write_into_text_file(FILE *text_file, STUDENT_DATA *a_student);
int main(int argc, char *argv[]) {
//getting arguments
if (argc != 4) {
printf("Enter:\n");
printf("%s ", argv[0]);
printf("\"Source Text File Name\" ");
printf("\"Destination Text File Name\" ");
printf("\"Destination Binary File Name\" ");
// source file name
//destination text file name
// destination binary file name
return 1;
}
char *src_text_file_name = argv[1];
char *dst_text_file_name = argv[2];
char *binary_file_name = argv[3];
STUDENT_DATA a_student;
FILE *text_file;
FILE *binary_file;
// Read from txt, write into binary
printf("\nRead from text file and write into file started...\n");
// Open related file
if (!(text_file = fopen(src_text_file_name, "r"))) {
printf("\nCannot open %s\n", src_text_file_name);
return 1;
}
if (!(binary_file = fopen(binary_file_name, "wb"))) {
printf("\nCannot open %s\n", binary_file_name);
return 1;
}
while (read_from_text_file(text_file, &a_student)) {
write_into_binary_file(binary_file, &a_student);
}
// Call related function to read and write
// Close related files
fclose(text_file);
fclose(binary_file);
// Give information to the user
printf("\nRead from text file and write into binary file completed!\n");
// Read from binary, write into txt
printf("\nRead from binary file and write into text file started...\n");
// Open related file
if (!(binary_file = fopen(binary_file_name, "rb"))) {
printf("\nCannot open %s\n", binary_file_name);
return 1;
}
if (!(text_file = fopen(dst_text_file_name, "w"))) {
printf("\nCannot open %s\n", dst_text_file_name);
return 1;
}
while (read_from_binary_file(binary_file, &a_student)) {
write_into_text_file(text_file, &a_student);
}
// Call related function to read and write
// Close related files
fclose(text_file);
fclose(binary_file);
// Give information to the user
printf("\nRead from binary file and write into text file completed!\n");
// Open related file
// Call related function to read and write
// Close related files
// Give information to the user
// 2D array dynamic allocation example
int **grades_matrix;
// Get memory with malloc
grades_matrix = (int **) malloc(5 * sizeof(int *));
// Read src_text_file_name
if (!(text_file = fopen(src_text_file_name, "r"))) {
printf("\nCannot open %s\n", src_text_file_name);
return 1;
}
// Fill the matrix with Exams and HW grades
// rows are students; columns are Exams and HWs totally 6 grade
for (int i = 0; i < 5; i++) {
read_from_text_file(text_file, &a_student);
grades_matrix[i] = (int *) malloc(6 * sizeof(int));
for (int j = 0; j < 3; j++) {
grades_matrix[i][j] = a_student.exams[j];
grades_matrix[i][3 + j] = a_student.hws[j];
}
}
// Print the 2D array
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
printf("%d ", grades_matrix[i][j]);
}
printf("\n");
}
// Free the memory
for (int i = 0; i < 5; i++) {
free(grades_matrix[i]);
}
free(grades_matrix);
return 0;
}
BOOL read_from_text_file(FILE *text_file, STUDENT_DATA *a_student) {
int amount_read;
amount_read = fscanf(text_file, "%s %s %d %d %d %d %d %d %c", a_student->name, a_student->id, &a_student->exams[0],
&a_student->exams[1], &a_student->exams[2],
&a_student->hws[0], &a_student->hws[1], &a_student->hws[2], &a_student->letter_grade);
if (amount_read == 9) return TRUE;
else return FALSE;
}
BOOL write_into_binary_file(FILE *binary_file, STUDENT_DATA *a_student) {
int amount_written;
amount_written = fwrite(a_student, sizeof(STUDENT_DATA), 1, binary_file);
if (amount_written != 1) return FALSE;
else return TRUE;
}
BOOL read_from_binary_file(FILE *binary_file, STUDENT_DATA *a_student) {
int amount_read;
amount_read = fread(a_student, sizeof(STUDENT_DATA), 1, binary_file);
if (amount_read == 1) return TRUE;
else return FALSE;
}
BOOL write_into_text_file(FILE *text_file, STUDENT_DATA *a_student) {
int amount_written = fprintf(text_file, "%-26s %-5s %3d %3d %3d %3d %3d %3d %c", a_student->name, a_student->id,
a_student->exams[0], a_student->exams[1], a_student->exams[2],
a_student->hws[0], a_student->hws[1], a_student->hws[2], a_student->letter_grade);
if (amount_written < 0) return FALSE;
else return TRUE;
}
//
// Created by User on 2.06.2022.
//