seperate and read a txt file

This is txt file

1P -------------------> class name
4 ---------------------> number of students
Catlin HENDERSON 100
Tessy BARNETT 101
Toma RICE 102
Adora ORTIZ 112
2H ------------------> class name
3 --------------------> number of students
Adora ORTIZ 112
Adora ORTIZ 110
Shani ROSE 112
3Q ---------------------> class name
2 -------------------> number of students
Bathsheba MEYER 105
Dix BUTLER 111
4G
4
Greer FORD 106
Barby SHAW 107
Trude ANDERSON 108
Alla HARRIS 109
5H
0

i have className's cn and studentsNumber's n

1
2
3
4
5
6
7
8
9
10
11
class className
{
public:
	string cn;
};

class studentsNumber
{
public:
	int n;
};


and i want to send

"class name" to className and
"number of students to "studentsNumber" class

how can i do that with ifstream
Last edited on
1
2
3
4
5
6
7
ifstream fin("file.txt");

std::string className; // presumably defined in the class
fin >> className;

int studentsNumber; // presumably defined in the class
fin >> studentsNumber;


Edit: With the classes you just edited in:
1
2
3
4
5
6
7
ifstream fin("file.txt");

className the_className;
fin >> the_className.cn;

studentsNumber the_studentsNumber; 
fin >> the_studentsNumber.n;

Your classes are rather silly, why not just use a string and int?
Last edited on
because i'm first year programming and this is a part of my homework :D
Maybe like this
1
2
3
4
5
class Student {
    string fname;
    string lname;
    int marks;  // or whatever that number is.
};


Then a class would be
1
2
3
4
class Class {
    string name;
    vector<Student> students;
};


And several classes make a department, say
1
2
3
class Department {
    vector<Class> classes;
};
thanks but i shouldn't use vector
when i do that

1
2
3
4
5
6
7
8
9
10
	ifstream fin("file.txt");

	className the_className;
	fin >> the_className.cn;

	studentsNumber the_studentsNumber;
	fin >> the_studentsNumber.n;

	 cout << the_className.cn << endl;
	 cout << the_studentsNumber.n << endl;


it shows only

1P
4

i need

1P
4
2H
3
3Q
2
4G
4
5H
0


should I create my own algorithm or is there an easy solution
Last edited on
don't over-think this :)

while you can getline,
if the line you got has a length of 2
{ print it
get another line
print that one too
}

I am assuming that YOU added the -------------------> class name and that the file does not actually have these tokens.
Last edited on
logarech,
Yes, I only showed you an example of how to get the first class name and number of students.
Think about the information this gives you now: You now know the number of students, which corresponds to the number of lines in your file before the next class name/number of students shows up.

Hint: You can use getline(ifstream, string). Combine it with a loop that loops [number of students] times.
https://www.geeksforgeeks.org/getline-string-c/
which corresponds to the number of lines in your file before the next class name/number of students shows up.

this is true, and nicer than the string length of 2 that I suggested, and is probably what they actually wanted you to do. I was *that* student, didn't really take classroom problems too seriously.
Last edited on
Topic archived. No new replies allowed.