#include <iostream>
#include <stdio.h>
using namespace std;
struct Table{
char fullname [100];
int group;
int firstMarks[5];
int secondMarks[5];
};
void ReadTable (Table *&data, int N);
void ShowTable (Table *data, int N);
int main() {
int N = 5;
Table *data = new Table[N]{
{},
{},
{},
{},
{}
};
ReadTable(data, N);
ShowTable(data, N);
}
void ReadTable (Table *&data, int N){
FILE *fl = fopen("table.txt","r");
if (!fl) {
cout << "Помилка";
exit(1);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
fscanf(fl, "%[^|] | %d | %d %d %d %d %d | %d %d %d %d %d ", &data[i].fullname, &data[i].group,
&data[i].firstMarks[j], &data[i].firstMarks[j], &data[i].firstMarks[j], &data[i].firstMarks[j], &data[i].firstMarks[j],
&data[i].secondMarks[j], &data[i].secondMarks[j], &data[i].secondMarks[j], &data[i].secondMarks[j], &data[i].secondMarks[j]);
}
fscanf(fl, "\n");
}
fclose(fl);
}
void ShowTable (Table *data, int N){
for (int i = 0; i < N; i++) {
cout << data[i].fullname << " ";
cout << " | ";
cout << data[i].group << " ";
cout << " | ";
for (int j = 0; j < N; j++) {
cout << data[i].firstMarks[j] << " ";
}
cout << " | ";
for (int j = 0; j < N; j++) {
cout << data[i].secondMarks[j] << " ";
}
cout << endl;
}
}
file table.txt:
Новикова Аделина Максимовна | 1151 | 5 2 3 1 2 | 1 2 1 3 2
Воронина Анна Леоновна | 2151 | 2 4 3 2 1 | 5 4 5 5 4
Медведева Елена Михайловна | 4452 | 4 2 1 3 4 | 2 4 5 2 1
Зайцев Пётр Миронович | 1148 | 2 3 4 5 1 | 5 2 1 5 5
Кочергин Алексей Семёнович | 3252 | 4 4 3 4 5 | 2 1 3 4 2
Console output:
Кочергин Алексей Семёнович | 3252 | 2 1 4 1 5 | 2 4 1 5 2
| 0 | 0 0 0 0 0 | 0 0 0 0 0
| 0 | 0 0 0 0 0 | 0 0 0 0 0
| 0 | 0 0 0 0 0 | 0 0 0 0 0
| 0 | 0 0 0 0 0 | 0 0 0 0 0
Hi all, there is a table, one row of the table corresponds to one student and contains the following information: student's name, group number, exam grades for the first semester, exam grades for the second semester.
How do I read the information from the file into my structure variables? My attempts are unsuccessful, hope for your help!