reading from a file and writing to a file

Hi I am trying to read student records from a file to an array of Student objects and I need to write those object to a file.
I can read the lines from the file, I can see each line in debug being read.
How ever my array only takes the first element of the each file for some reason.
It does not get the other elements.

And for writing, in debug I can see array has correct data in it but I can't get it to print it to a file.


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
30
31
32
33
34
35
36
37
38
  Student* readStudentsFromFile(string filename, int num) {
	ifstream studentsStream;
	studentsStream.open(filename.c_str());
	
	string line, name, school, sid;
	int id;

	Student* readArray = new Student [num]; //An array of student object that will contain the read objects
	
	if (!studentsStream.is_open()) {
		cerr << "Couldn't open the file " << filename << endl;
		return NULL;
	}
	
	//while (!studentsStream.eof()) 
		
		for(int i=0;i<num;i++) {

			getline(studentsStream, line); 
			istringstream iss(line);
			getline(iss, name, ',');
			getline(iss, sid, ',');
			getline(iss, school, ' ');
			istringstream idConv(sid);
			idConv >> id;
			
                         //This is array only gets the first line of the file for some reason
			readArray[i] = Student(id,name,school); //An array of Student objects
			
			cout<<readArray<<"\n";	

		}//End of for loop
	//}//End of while
		
	studentsStream.close();

	return readArray;
}//End of readStudentsFromFile 




This is suppose to write those students to a file but I can't get the data to print
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void writeStudentsToFile(Student students[], int num, string filename) {

	ofstream output(filename.c_str());
	output.open(filename);
	
	
	for(int i=0;i<num;i++) {
		 output << students[i].toString();
	}//End of for loop



	output.close();
}//End of writeStudentsToFile 


This is the toString function in the class to convert the integer id number to string for printing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string Student::toString() {

	string str;						

	ostringstream convert;		//Converting the ID number to string
	convert << Fid;				//	
	str=convert.str();			//

	string toString="";

	return toString.append(Fname).append(",").append(str).append(",").append(Fschool);//Returning added strings
}




And this is the class implementation to show what Student class takes as args
1
2
3
4
5
6
7
Student::Student (int id, string name, string school) {

	Fid = id;
	Fname = name;
	Fschool = school;

}

Why are you using an array instead of a std::vector? Where and to what value is "num" initialized?

Look at the following snippet:
1
2
	ofstream output(filename.c_str());
	output.open(filename);

Why are you trying to open the same file, using the same stream twice? You don't need that second line.

You should also be checking to insure the file actually closed properly and you don't need to close the file, it will close properly when the function returns and the stream goes out of scope.

Next look at the following snippet:

1
2
3
4
5
6
7
	ostringstream convert;		//Converting the ID number to string
	convert << Fid;				//	
	str=convert.str();			//

	string toString="";

	return toString.append(Fname).append(",").append(str).append(",").append(Fschool);//Returning added strings 


Why us all those appends? you already have a plenty good ostringstream, why not use that instead?

1
2
3
4
5
6
  ostringstream convert;
   
   convert << Fname << ',' << Fid << ',' << Fschool;

   return convert.str();
}


Lastly please post a sample of your input file.

Last edited on
We are not allowed to use vectors yet.
"num" is a const and it is initialized in main before the function call.
Number of students in each file and number of common students in each file is given so that is not the problem.
I think toString works fine and it is the output that causes the problem or the data itself.
Because when I use cout to print array with toString it works.
I am suppose to find the common students by linear search and binary search and print each into different files.
I think it is the linear search function that causes the printing problem.

This is the linear search funtion.
len1,len2 and numCommon are consts and initialized in the main before funtion call.
Group 1 is from sample file 1 and group 2 is from sample file 2.
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
30
31
32
33
34
35
36
37
38
39
Student* findCommonStudents1(Student group1[], int len1, Student group2[],
		int len2, int numCommon) {
	
	Student *commonArray1 = new Student[numCommon];//An array that will hold the common students
	
	int counter=0;
	

	for(int i=0;i<len1;i++) {
		
		for(int a=0;a<len2;a++) {
			

			if(group1[i]==group2[a]) {
			
				commonArray1[counter]=group1[i];
				counter++;

				cout<< commonArray1[counter].toString()<< "<<this is commonarray1[counter]\n";
				cout<< group1[i].toString()<<"this is group1[i]\n";
				cout<<group2[a].toString()<<"this is group2[a]";
	
			}//enf of if statment
			
			

		}//end of inner for loop
	



	}//end of outer for loop




	return commonArray1;

}//end of findCommonStudents1 


Sample file 2
1
2
3
4
5
Junior Lesa,50777409,SMC
Caroline Greene,10537229,SMC
Jonathan Prindle,28152842,SMC
Roslyn Brooks,22829605,SMC
Zoila Saa,66620395,SMC

Sample file 1
1
2
3
4
5
6
7
8
9
10
Jamar Gosman,43901450,UCB
Jonathan Prindle,28152842,UCD
Rebeca Zoebisch,28149026,UCSD
Jackson Meredith,65030459,UCSF
Vilma Eid,42237807,UCM
Zoila Apolo,23070708,UCD
Gertrude Goldyn,66737716,UCLA
Jeannie Schnathorst,41549126,UCD
Maricela Facemire,97606808,UCSD
Zoila Saa,66620395,UCSF

I think it is the linear search function that causes the printing problem.

Have you run the program through your debugger to determine if this is the cause. Place a breakpoint at the beginning of this function and single step through the function, watching the variables as you step.

The following line is of concern to me:

if(group1[i]==group2[a]) {

You probably should be comparing the identification number of each student instead of the of the whole object.

Oh, and are you deleting all that memory you're newing when you're finished with it? You should always delete[] what you new[].

I think toString works fine and it is the output that causes the problem or the data itself.
I never said it didn't work, I just asked why you're making it so complex.
YEEES it works now.

I overloaded the == operator so that I can do if(group1[i]==group2[a]).

Problem was the counter++ in the linear search funtion.

Do I have to delete arrays that I make inside the funtions too?
I thought they go away after the calls?
What you new you must delete. It doesn't matter where you dynamically allocate memory you need to release the memory.
Topic archived. No new replies allowed.