reading files

I am trying to read a file with format below. They are info of students. Data fields are separated by the percent (%) character. How do i make the program read the file with fstream? will it be able to skip % automatically?
Thank You
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  HOI, Yee So%PSYC1001%55%GEOG1016%47%POLI2107%71%ECON1210%77%FINE1001%72
TAM, Siu Wah%POLI0079%72%SOCI2001%66%PSYC2060%68%THAI1001%77%GEOG1017%69

struct course_record { 

string number;  
int mark; 

}; 

struct student_record { 

 string name; // name of student 
course_record course[5]; //courses taken by student 

}; 
There are many ways of doing this, I will demonstrate operator overloading method:
http://coliru.stacked-crooked.com/a/ed4d50127ef90325

I don't really understand your method. Is there a simpler way to do so?
Yes, there is the string::find and string::substr method. Although, I'm not sure if it is simpler:

http://coliru.stacked-crooked.com/a/8b4e6026973b6b74

You can also take a look at this thread on SO that includes many fancy ways of splitting a string:
https://stackoverflow.com/questions/236129/how-to-split-a-string-in-c

Wonder when the string class will finally get it's own split method. As for now I guess profs will continue to use this as a little practice exercise
Last edited on
Topic archived. No new replies allowed.