-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_file.cpp
More file actions
74 lines (61 loc) · 1.73 KB
/
read_file.cpp
File metadata and controls
74 lines (61 loc) · 1.73 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
#include "read_file.h"
#include <iostream>
using namespace std;
Rating* resize(Rating* cur){
Rating *next = new Rating(cur -> size + 10, 64);
for (int i = 0; i < cur -> size; i++){
next -> surnames[i] = cur -> surnames[i];
}
for (int i = 0; i < cur -> size; i++){
next -> subjects[i] = cur -> subjects[i];
}
for (int i = 0; i < cur -> size; i++){
next -> isContract[i] = cur -> isContract[i];
}
return next;
}
Rating* read_file(char *fileName) {
FILE *pFile;
char buffer[125];
pFile = fopen(fileName, "r");
if (pFile == NULL) {
perror("Error opening file");
return NULL;
}
else {
int size = 10;
Rating* CurRating = new Rating(size, 64);
int i = 0;
while (fgets(buffer, 125, pFile)){
if (CurRating -> size - i == 0){
CurRating = resize(CurRating);
}
parse_buffer(buffer, CurRating, i);
if (i == 79) break;
i++;
}
fclose(pFile);
return CurRating;
}
}
void parse_buffer(char* buffer, Rating* CurRating, int curIndex) {
char * ptr1 = strtok(buffer, ",");
char * ptr2 = ptr1;
if (ptr1 != NULL) {
ptr1 = strtok(NULL, ",");
strncpy(CurRating->surnames[curIndex], ptr2, ptr1 - ptr2);
}
char *substr = new char[10];
for (int i = 0 ; i < 5 ; i++) {
ptr2 = ptr1;
ptr1 = strtok(NULL, ",");
strncpy(substr, ptr2, ptr1 - ptr2);
int value; sscanf(substr, "%d", &value);
CurRating->subjects[curIndex][i] = value;
}
ptr2 = ptr1;
while ((*ptr1) != '\0') ptr1++;
strncpy(substr, ptr2, ptr1 - ptr2);
CurRating->isContract[curIndex] = strlen(substr) < 6 ? 1 : 0;
delete []substr;
}