need help understanding extracting from file and loading into array.






This what i have so far i have hard time understanding extract and even less luck with understanding how to turn the extract info into an array to be searched



1
2
3
4
5
6
7
8
9
10
11
12
13
void extract()
{
	fin.open("E\\:studentidarray.txt");
	fin >> studentid >> lastname >> firstname >> gpa;
	

	
	fin.close();




}
Last edited on
">>" operators separate words by the spaces.

In your case, you need to have the information seperated by spaces. In your studentidarray.txt file, you either must have:

001 Johnson Samantha 3.22


Or you could have

001
Johnson
Samantha
3.22


Or you could have a mixture of both

001
Johnson Samantha
3.22


The point is that you separate data either by endlines (I.E. "\n") or spaces.

Edit: Note that this is currently only exacting one student from the file. Trying to extract multiple students may be a bit trickier. Something like this would work.

1
2
3
4
5
6
7
8
9
10
int studentid;
char lastname[256];
char firstname[256];
double gpa;

while (fin >> studentid) {
    fin >> lastname >> firstname >> gpa;
    
    // Insert this data into a vector, array, or display it.
}


Hope that helps!
Last edited on
i have the file printing on seperate lines for each set of studentid, lastname, firstname, gpa. and there is a space between each of the 4
Topic archived. No new replies allowed.