reading txt file into class and array



i want to read a txt file with the text below

14587569A Peter 3.24 10 Nil
13440531A Billy 3.95 11 HKU-CS

What should the codes be like if I want to modify some of the attributes after reading the text in array ?


For example, like modifying f the above text to the following text
14587569A Peter 3.45 10 PolyU-Computing
13440531A Billy 3.95 11 HKU-CS

I would appreciate if you could help. Thanks a lot.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Readin
{
	string id;
	double name;
	double number1;
	int number2;
	string code;
};

int main()
{
	ifstream fin;
	Readin list[2];

	fin.open("textfile.txt");
	
	for (int x = 0; x < 2; x++)
		fin >> list[x].id >> list[x].name >> list[x].number1 >> list[x].number2 >> list[x].code;

	fin.close();

	return 0;
}


For changing anything in the txt file you call the part you want such aslist[0].id = "14587569A" or you could do though the screen and type it in
1
2
3
string input;
fin input;
list[0].id  = input;


or

1
2
for (int x = 0; x < 2; x++)
	cin >> list[x].id >> list[x].name >> list[x].number1 >> list[x].number2 >> list[x].code;
Last edited on
Topic archived. No new replies allowed.