Files

Reading from two files

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2188 Views - Last Post: 08 June 2010 - 03:28 AM Rate Topic: -----

#1 ahaykal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-April 09

Files

Posted 07 June 2010 - 10:45 AM

Hello again,

I will go straight to the point. I have two files one that is called student.dat and the other is called grades.dat .
Student.dat has the following items in it :

20092112 Anthony Haykal
20084564 John Doe
20075640 James Bond
20045678 Cynthia Smith
20092134 Jennifer Hajj

grades.dat has these :
20045678 75 80 85
20092134 64 51 84
20075640 57 65 90
20092112 80 90 85
20084564 85 46 68

The question asks me to read the data from “grades.dat” file and print on the screen the Student’s ID and Students’s Name (first name, last name) and the total grade.
You will find the firstname and the lastname in the “student.dat” file. (Note that
the files “student.dat” and “grades.dat” are NOT in the same order.)

I just have a problem with the order. I mean how do i make like id number 20092112 to print the name and final name in this case Anthony Haykal
while showing the grades also. If i read them in sequential order i cant like point to this specific id. I arrived here and got stuck :s

#include <iostream>
#include <fstream>
using namespace std;
int main()
{//start main
	int id;
	int id2;
	
	
	char FirstName[30];
	char LastName[30];
	int AssignmentGrade;
	int MidTermGrade;
	int FinalGrade;
	int student[5];


	ifstream infile("grades.dat" , ios::in);
	ifstream infile2("student.dat", ios::in);
	if (!infile||!infile2)
	{
	cout<<"Could not open the file(s)";
	return 1;
	}

	

	while (infile>>id>>AssignmentGrade>>MidTermGrade>>FinalGrade)
				{//first while
	


while(infile2>>id2>>FirstName>>LastName)
	
{//second while


if (id == id2)
{
cout<<FirstName<<endl;
}
else		if (id != id2)
			cout<<"Not same id"<<endl;

		



}//second while




	}//first while

	cout<<endl<<endl;






infile.close();
infile2.close();








}//end main



Could you aid me with this issue please!
Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Files

#2 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Files

Posted 07 June 2010 - 10:51 AM

Draw out a design on paper, you probably have books on software engineering you should be reading.

But, if not.. Draw it out on paper, how it needs to work.


I expect a rough draft of your software in 15 minutes. Do include what classes you will be using, and their relationship to other classes. (CRC CARD) Pay special attention to the interfaces also, and if you don't need to use classes, make it interesting.

This post has been edited by taylorc8: 07 June 2010 - 10:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 RedSon   User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: Files

Posted 07 June 2010 - 10:51 AM

You may want to consider creating some helper classes or using a more advanced data structure.

You have some kind of ID number in both files. Maybe you could create a class called Student, and in that Student class you have an ID, First Name and Last Name. Then you create another class called Grade, and in that you have an ID, Assignment, Midterm and Final. Then maybe you have another class called "StudentGroup" and in that class you have an array of Students and an array of Grades. Then when you read in from students you would iterate over the array of students and populate the data. Likewise for the grades array. Then when you go to look at the student's grades you can look by ID, find the student with ID 12345, and the grade with ID 12345.
Was This Post Helpful? 0
  • +
  • -

#4 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Files

Posted 07 June 2010 - 10:56 AM

struct StudentRecord
{
std::string id;
std::string name;
};


is probably something similar to what I would plan to use in my first draft.

You'll probably need to break this problem down into more manageable pieces.

This post has been edited by taylorc8: 07 June 2010 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#5 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: Files

Posted 07 June 2010 - 11:01 AM

I would maybe read in the names in to a vector, aswell as the grades, and once the names and grades file have been read thoroughly through and all data has been stored into the vectors, just iterate through each vector and print each data member sequentially.

for(/*some condition*/)
{
   if(/*some condition*/)
   {  
      cout << /*student name*/ \t /*student score*/ << endl;
    }
}



You may not even need the if statement.
Was This Post Helpful? 0
  • +
  • -

#6 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Files

Posted 07 June 2010 - 11:19 AM

One problem I think you are having is that you don't understand how file streams work. When you open a file, you are at the beginning. As you read, you always move forward and never go back. So your loops might work for the first id in grades.dat. At this point, student.dat is at the beginning. But, durring the second while loop, you move forward in student.dat. Once you try to match the second id, your infile2 (student.dat) is already in the middle or at the end of the file before you even enter the second while loop for the second time. You could reset to the beginning of the file (look up seekg() and setg()), but I would recommend just reading the data from both files into arrays then working with those arrays instead of constant file IO.
Was This Post Helpful? 0
  • +
  • -

#7 ahaykal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-April 09

Re: Files

Posted 07 June 2010 - 11:31 AM

first off thanks for the reply.

@mojo666 u r right i got confused a bit at the start with where the cursors is i understood your point. I tried to read the data and put them in arrays. like arrays[i] = id. but then when i output it shows everything including the first name and last name.

@ingenious hax i really dont know how to use vectors we still didnt take them

@redson the instructor told us not to use classes nor structs

thanks again
Was This Post Helpful? 0
  • +
  • -

#8 RedSon   User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: Files

Posted 07 June 2010 - 12:10 PM

View Postahaykal, on 07 June 2010 - 09:31 AM, said:

@redson the instructor told us not to use classes nor structs


That is the goofiest thing I have ever heard. Did they give you a reason why? Or maybe a hint as to how you are going to solve it instead?
Was This Post Helpful? 0
  • +
  • -

#9 ahaykal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-April 09

Re: Files

Posted 07 June 2010 - 12:17 PM

View PostRedSon, on 07 June 2010 - 11:10 AM, said:

View Postahaykal, on 07 June 2010 - 09:31 AM, said:

@redson the instructor told us not to use classes nor structs


That is the goofiest thing I have ever heard. Did they give you a reason why? Or maybe a hint as to how you are going to solve it instead?


it is one of the goofiest things ive heard but thats the truth. All the semester we were taking about classes and had lots of assignments about them and now we cant use them :s(You can check my previous posts i asked about some assignments if you dont believe me ). My only problem is just matching the id to the student from the other file :s.

thanks for replying again!
Was This Post Helpful? 0
  • +
  • -

#10 RedSon   User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: Files

Posted 07 June 2010 - 12:21 PM

View Postahaykal, on 07 June 2010 - 10:17 AM, said:

View PostRedSon, on 07 June 2010 - 11:10 AM, said:

View Postahaykal, on 07 June 2010 - 09:31 AM, said:

@redson the instructor told us not to use classes nor structs


That is the goofiest thing I have ever heard. Did they give you a reason why? Or maybe a hint as to how you are going to solve it instead?


it is one of the goofiest things ive heard but thats the truth. All the semester we were taking about classes and had lots of assignments about them and now we cant use them :s(You can check my previous posts i asked about some assignments if you dont believe me ). My only problem is just matching the id to the student from the other file :s.

thanks for replying again!

No, its not that I don't believe you its just that I am incredulous that a teacher would not allow you to use one of the main points of OOP, esp. if you just finished learning about it.

Did the teacher give you a suggestion on how to solve it otherwise?
Was This Post Helpful? 0
  • +
  • -

#11 ahaykal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-April 09

Re: Files

Posted 07 June 2010 - 12:51 PM

View PostRedSon, on 07 June 2010 - 11:21 AM, said:

View Postahaykal, on 07 June 2010 - 10:17 AM, said:

View PostRedSon, on 07 June 2010 - 11:10 AM, said:

View Postahaykal, on 07 June 2010 - 09:31 AM, said:

@redson the instructor told us not to use classes nor structs


That is the goofiest thing I have ever heard. Did they give you a reason why? Or maybe a hint as to how you are going to solve it instead?


it is one of the goofiest things ive heard but thats the truth. All the semester we were taking about classes and had lots of assignments about them and now we cant use them :s(You can check my previous posts i asked about some assignments if you dont believe me ). My only problem is just matching the id to the student from the other file :s.

thanks for replying again!

No, its not that I don't believe you its just that I am incredulous that a teacher would not allow you to use one of the main points of OOP, esp. if you just finished learning about it.

Did the teacher give you a suggestion on how to solve it otherwise?


not really no. I will keep on trying though ill give u my last attempt tomorrow to see my progress :). any suggestions are welcome :)
Was This Post Helpful? 0
  • +
  • -

#12 Banfa   User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: Files

Posted 07 June 2010 - 12:56 PM

Well I would do something like create a function that would take a student id number and open and read the name file until it matched the number to a name then close the name file and returned the name.

This does mean that for every record in the data file you would be opening and reading the name file but it does mirror the real life situation where the data files are so big that you don't really want to load them all into memory. Of course in such a case in real life I would actually cache the last N records read from the name file but since such a cache would almost certainly use a class which you can't I would forget about it in this case.
Was This Post Helpful? 2
  • +
  • -

#13 Cerolobo   User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Files

Posted 07 June 2010 - 01:06 PM

I'll go ahead and throw my hat into the ring. From what I can tell, it seems to me that your instructor is attempting to teach you guys a bit about File I/O.

Under the above assumption, I would do what Banfa suggested. Namely search the file for the specified student number. This will be slow, but it'll cover a lot of File I/O related topics. Namely,
..Opening and Closing a file
..Seeking in a file (not required)
..Fragmenting the text into parts (ID, Name, Grades)...
..Handling Multiple Files
Was This Post Helpful? 0
  • +
  • -

#14 ahaykal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-April 09

Re: Files

Posted 07 June 2010 - 01:12 PM

@Banfa thats a good idea thanks.

I will have to hand it to her after tomorrow! so i will work on it tomorrow

to all thanks for your help i will tell u if i face any other problem thanks again!
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Files

Posted 07 June 2010 - 01:17 PM

lacking classes or structs is a logical problem but not much more... structs are after all just syntactic sugar -- by that I mean that they make things convienent but are not really necessary.

Note: What follows is not meant to be advice on how to solve your problem unless you are just really interested in irritating your teacher :P

Example:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>


using namespace std;

/* using an enum and array to simulate a struct
 *
 * struct Strudent {
 *     int id;
 *     char lname[20];
 *     char fname[20];
 * }; */

//Step 1: Define an enum to mark points in our buffer
enum {
    StudentRecord_ID = 0,
    StudentRecord_FName = StudentRecord_ID + sizeof(int),
    StudentRecord_LName = StudentRecord_FName + 20*sizeof(char),
    StudentRecord_SIZE = StudentRecord_LName + 20*sizeof(char),
};

//step 2 (optional) make a nice typedef to define an array of the right size...
typedef char StudentRecord[StudentRecord_SIZE];

//Step 3: some access methods that cast various parts of the array to the correct type...
int& getStudentId(StudentRecord& record);
char* getStrudentLName(StudentRecord& record);
char* getStrudentFName(StudentRecord& record);


int getStrudentData(const char* filename, StudentRecord* records, int maxNum);
void displayRecord(StudentRecord& record);

int main() {
    StudentRecord students[20]; // an array of 20 students
    int num = getStrudentData("student.txt", students, 20);
    for (int i = 0; i < num; i++) {
        displayRecord(students[i]);
    }
    return 0;
}

//Get the student ID from the record (return as reference so you can assign)
//  same as: record.id
int& getStudentId(StudentRecord& record) {
    return *((int*)record);
}

//Get the student LName from the record (return as pointer)
//  same as: record.lname
char* getStrudentLName(StudentRecord& record) {
    return (char*)(record+StudentRecord_LName);
}

//Get the student FName from the record (return as pointer)
//  same as: record.fname
char* getStrudentFName(StudentRecord& record) {
    return (char*)(record+StudentRecord_FName);
}


int getStrudentData(const char* filename, StudentRecord* records, int maxNum) {
    ifstream data;
    data.open(filename);
    int count = 0;
    if (data) {
        while(data && count < maxNum) {
            data >> getStudentId(records[count]);
            data >> setw(20);
            data >> getStrudentFName(records[count]);
            data >> setw(20);
            data >> getStrudentLName(records[count]);
            if (data) {count++; }
        }
    } else {
        cerr << "file did not open [" << filename << "]\n" << endl;
    }
    data.close();
    return count;
}

void displayRecord(StudentRecord& record) {
    cout << getStudentId(record) << " - "
         << getStrudentLName(record) << ", "
         << getStrudentFName(record) << endl;

}



Tada! No struct yet I kept each record bound into a nice little package...


For your assignment you will probably want to keep an array for each item and use a common index as your linking component...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2