forked from joel1449/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadtest.cpp
More file actions
79 lines (76 loc) · 1.46 KB
/
readtest.cpp
File metadata and controls
79 lines (76 loc) · 1.46 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class fileRead
{
private:
ifstream ifs;//read from file
ofstream ofs;//write to file
string line; //temporary line holder
public:
void exam()
{
int temp_reg;
float av_score;
cout << "Enter Reg No: ";
cin >> temp_reg;
auto foundLine = searchLine(std::to_string(temp_reg));
cout << "Details: " << foundLine << std::endl;
cout << "\n\n\tEnter students' Average Score: ";
cin >> av_score;
appendToFile(foundLine , std::to_string(av_score));
}
void appendToFile(string conLine , string Data)
{
ifs.open("studentsData.txt");
ofs.open("studentsExam.txt");
if(!ifs.is_open())
{
cerr << "The file could not open for file reading" << endl;
exit(-1);
}
else
{
while(getline(ifs ,line))
{
if(line == conLine)
{
line +=" " + Data;//add the data to the line
}
ofs << line << "\n";
}
}
ifs.close();
ofs.close();
}
string searchLine(string searchKey)
{
ifs.open("studentsData.txt");
if(!ifs.is_open())
{
cerr << "File Not open " << endl;
exit(-1);
}
string find_line , return_line;
while(getline(ifs,find_line))
{
if(find_line.find(searchKey , 0) != string::npos)
{
return_line = find_line;
}
else
{
continue;
}
}
ifs.close();
return return_line;
}
};
int main()
{
fileRead re;
re.exam();
return 0;
}