How to read from a file with varying amount and multiple data types in each line c++

Following is the file : ( The first line shows the number of Students in the file )

These are Names followed by marks in different subjects.

15
Albert Einstein 52 67 63
Steve Abrew 90 86 90 93
David Nagasake 100 85 93 89
Mike Black 81 87 81 85
Andrew Van Den 90 82 95 87
Joanne Dong Nguyen 84 80 95 91
Chris Walljasper 86 100 96 89
Fred Albert 70 68
Dennis Dudley 74 79 77 81
Leo Rice 95
Fred Flinstone 73 81 78 74
Frances Dupre 82 76 79
Dave Light 89 76 91 83
Hua Tran Du 91 81 87 94
Sarah Trapp 83 98


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
#include"student.h"

int main()
{
	string fileName,name;
	int sizeOfArray,i=0;
	cin>>fileName;
	
	Student *s;

	ifstream f1(fileName.c_str());
	if(!f1)
	{
		cout<<"\nERROR!!\nFILE NOT FOUND";
		exit(0);
	}
	else
	{
		f1>>sizeOfArray;
		s=new Student [sizeOfArray];
		while(!f1.eof())
		{
			f1>>name;
			i++;
		}
	}
}
Last edited on
You can basically ignore that 15 and just read the file normally. You need a structure to contain the data for each line though.
Yeah, I've read the 15 as sizeOfArray, thats not the issue, the issue is if you see in the text file, some names are 3 names and 4 marks and some are 2 names and 1 subject mark and so on,
What I want to know is how do I manipulate the data from the file such that every name goes completely into string name and every mark goes into every individual mark.
- Read each token into a string
- Try to read that string as a number
- If it's not a number, it's part of the name

If you know that names will always be exactly 2 or 3 words, that makes your life simpler too.

Also, see if anything from here helps at all:
http://www.LB-Stuff.com/user-input
Last edited on
Topic archived. No new replies allowed.