-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio.cpp
117 lines (102 loc) · 2.64 KB
/
fileio.cpp
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
#include "main.h"
/**
* saveDataToFile - saves collected data to a file
* @filename: name of the file to save to.
* @head: student node head
*
* Return: Nothing
*/
void saveDataToFile(const std::string& filename, StudentNode* head)
{
clearTerminal();
std::ofstream outputFile(filename);
if (!outputFile)
{
std::cout << "Error opening file for writing: " << filename << std::endl;
return;
}
// Write each student's data to the file
StudentNode* currentStudent = head;
while (currentStudent != nullptr)
{
outputFile << currentStudent->studentName << std::endl;
SubjectNode* currentSubject = currentStudent->subjects;
while (currentSubject != nullptr)
{
outputFile << currentSubject->subjectName << "," << currentSubject->score << "," << currentSubject->creditHours << "," <<currentSubject->letterGrade << std::endl;
currentSubject = currentSubject->next;
}
outputFile << std::endl; // Add empty line between students
currentStudent = currentStudent->next;
}
outputFile.close();
std::cout << "Data saved to file: " << filename << std::endl;
}
/**
* loadDataFromFile - load data from a file
* @filename: the filename to load from
* @head: head of the student structure
*
* Return: Nothing
*/
void loadDataFromFile(const std::string& filename, StudentNode*& head)
{
std::ifstream inputFile(filename);
if (!inputFile)
{
clearTerminal();
std::cout << "Error opening file for reading: " << filename << std::endl;
return;
}
// Clear existing student data
cleanMemory(head);
head = nullptr;
std::string line;
std::string studentName;
bool isNewStudent = true;
while (std::getline(inputFile, line))
{
if (line.empty())
{
isNewStudent = true;
continue;
}
if (isNewStudent)
{
studentName = line;
insertStudent(head, studentName);
isNewStudent = false;
}
else
{
std::istringstream iss(line);
std::string subjectName;
double score;
int creditHours;
if (std::getline(iss, subjectName, ',') &&
(iss >> score) &&
(iss.ignore(), iss >> creditHours))
{
std::string letterGrade = calcLetterGrade(score);
double gradePoint = calcGradePt(letterGrade);
insertSubject(findStudent(head, studentName)->subjects, score, creditHours, gradePoint, subjectName, letterGrade);
}
}
}
inputFile.close();
clearTerminal();
std::cout << "Data loaded from file: " << filename << std::endl;
}
/**
* loadData - load students data from a file
* @head: head for student linked list
*
* Return: Nothing
*/
void loadData(StudentNode*& head)
{
std::string filename;
std::cout << "Enter filename to load data: ";
std::cin >> filename;
loadDataFromFile(filename, head);
}