File IO within a Class

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1297 Views - Last Post: 14 September 2009 - 05:27 AM Rate Topic: -----

#1 muddturtle13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

File IO within a Class

Posted 13 September 2009 - 11:24 AM

This is very novice programming, just to warn you. It shouldn't be very difficult for those of you out there that are more experienced.

Basically, the program boils down to reading a file for input, displaying the raw data to the screen (cout), and then re-writing the raw data to a new output file with subtotals.

I'm all right on the logic. That isn't my problem. I've read and written files before in int main() with no worries before. I just have to do it this time completely within the classes, and I don't have the faintest clue how to go about this. Can anyone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: File IO within a Class

#2 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: File IO within a Class

Posted 13 September 2009 - 11:36 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: [code]code goes here [/code]

Now, have you worked with classes before?
Was This Post Helpful? 0
  • +
  • -

#3 muddturtle13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: File IO within a Class

Posted 13 September 2009 - 11:41 AM

Sorry, I am new here and still learning the ways of this site.

I have worked with classes before, but that was well over a year ago, and my instructor hardly reviewed us before throwing this assignment at us. My experience with classes does not include doing file input and output within the class itself.

Please note that my code is minimal and probably contains several errors.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct sStudent
{
	private:
		string studentID;
		string courseID;
		int hours;
		char grade;
}
		

class cStudent
{
	private:
		ifstream fileIn;
		ofstream fileOut;

	public:
		string studentID;
		string courseID;
		int hours;
		char grade;
		double gpa;
		bool end;

		cStudent(char []);
		~cStudent();
		bool readFile();
		void writeFile(string, int, double);
};

cStudent::cStudent(char name[])
{
	fileIn.open(name);
	if(fileIn.fail())
	{
		cerr << "File " << name << " does not exist" << endl;
		abort();
	}
}

cStudent::~cStudent()
{
	fileIn.close();
}

bool cStudent::readFile()
{
	fileIn >> studentID;
	end = fileIn.eof();

	while (!end)
	{
		cout << studentID;
		fileIn >> courseID;
		cout << courseID;
		fileIn >> hours;
		cout << hours;
		fileIn >> grade;
		cout << grade;

		

		fileIn >> studentID;
	}
}

void cStudent::writeFile(string n, int h, double g)
{
	fileOut.open("studentSummary.dat");
}

int main()
{
	struct sStudent;
	class cStudent;	
}



Like I said, the source code is incomplete. I wanted to do my file input and output before continuing on with the rest of my program.
Was This Post Helpful? 0
  • +
  • -

#4 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: File IO within a Class

Posted 13 September 2009 - 11:58 AM

As for errors i can see that you forgot ';' after the '}' in the structure and that you have a bool function that doesnt return anything.
Why is it declared as bool?
Was This Post Helpful? 0
  • +
  • -

#5 muddturtle13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: File IO within a Class

Posted 13 September 2009 - 12:03 PM

Ahhh, thank you. I was wondering about that compiler error.

As far as the bool function, I started putting code in there when I got frustrated. The code in there is just what I was going to use to cout the data from the input file to the screen. Our professor suggested we declare it as a bool function since it will be checking to make sure the file was read correctly and returning a True or False value. Judging by your response, what I did is either incomplete or incorrect... or both.
Was This Post Helpful? 0
  • +
  • -

#6 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: File IO within a Class

Posted 13 September 2009 - 12:16 PM

In that function you are missing the return True or False

I am not near a comp where i can do any testing so, is the way you are reading the file tested before? in any other programs you have made?

You know making a class is not hard, just think as if you where making simple functions, better yet why dont you make the program as simple as you can first using functions only and then when you have it done. Make the class get the functions in and change the very few things that have to be changed. Another thing is that i cant see where you are using the structure so im asuming you had a different idea for reading the file.
I wont be helpfull right now without my comp so lets say i helped you with the errors and now ima go find a compiler to help you later.

This post has been edited by poncho4all: 13 September 2009 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#7 muddturtle13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: File IO within a Class

Posted 13 September 2009 - 12:25 PM

I am creating my own .dat file for the purposes of this program, and that function will be the only one that tests the file itself. And I've done classes before, which is why this frustrates me so much. The only thing new to me is opening, reading, and outputting a file within a class. I understand the overall logic involved. I'm just bogged down by the smaller details of it, syntax and whatnot.

Actually now that I think about it, I might just change it to a void function and just do the file check within the function. So, something like...

void cStudent::readFile()
{
	fileIn >> studentID;
	end = fileIn.eof();

	while (!end)
	{
		cout << studentID;
		fileIn >> courseID;
		cout << courseID;
		fileIn >> hours;
		cout << hours;
		fileIn >> grade;
		cout << grade;	

		fileIn >> studentID;
		end = fileIn.eof();
	}
}



So, I can just run the while loop if the file is not at the end. Then I can cout the data as I read it in. Make sense?
Was This Post Helpful? 0
  • +
  • -

#8 muddturtle13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: File IO within a Class

Posted 13 September 2009 - 12:52 PM

View Postponcho4all, on 13 Sep, 2009 - 11:16 AM, said:

You know making a class is not hard, just think as if you where making simple functions, better yet why dont you make the program as simple as you can first using functions only and then when you have it done. Make the class get the functions in and change the very few things that have to be changed. Another thing is that i cant see where you are using the structure so im asuming you had a different idea for reading the file.
I wont be helpfull right now without my comp so lets say i helped you with the errors and now ima go find a compiler to help you later.

I don't have a problem writing most of the functions. My main problem is the syntax of modifying files within a class.

I still have to call the functions and whatnot within int main(), yes? And I have to specify the name of the file being open or written to. Do I do that in the class or int main... both, maybe? These are the kinds of things that worry me most about this program.
Was This Post Helpful? 0
  • +
  • -

#9 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: File IO within a Class

Posted 13 September 2009 - 05:14 PM

Quote

I still have to call the functions and what not within int main(), yes? And I have to specify the name of the file being open or written to. Do I do that in the class or int main... both, maybe? These are the kinds of things that worry me most about this program.

You can do the specifying of the name of the file in the class or in the main
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

class OpenFile{
private: ifstream file1;
		 ofstream file;
public:
	void getdata();
};
void OpenFile::getdata(){
	file1.open("test.txt");
	file.open("test1.txt");
	string line;
	while(!file1.eof()){
		getline(file1, line);
		file<<line<<endl;
	}
}
int main(){
	OpenFile a;
	a.getdata();
	return 0;
}

That opens a file and sends info to another file
Its just a basic example, is this what we are talking about?
Was This Post Helpful? 0
  • +
  • -

#10 muddturtle13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: File IO within a Class

Posted 13 September 2009 - 06:37 PM

Yes, actually. That's very helpful. So, with that out of the way, I have opening the file and cout-ing the contents just fine. However, the last part is the most difficult.

Just to be clear, I'm reading in student records, which include... A student ID number (string), the student's course ID (string), the amount of hours the student is taking (int), and the grade the student received (char). Note that one student may take multiple courses. The cout is simple since I just output what I read in from the file.

However, I have to write an output file that lists a student's number, the total amount of hours he is taking, and his total gpa. I know how to calculate it and keep running totals. But should I be read the input file again for that information? Keep in mind I need a separate function for writing a file. Can I create a writeFile() method within student class and call it as I input data? This is the part that really gets me.
Was This Post Helpful? 0
  • +
  • -

#11 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: File IO within a Class

Posted 13 September 2009 - 06:59 PM

Could you post your new code?

And sure you can write a writeFile()
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

class OpenFile{
private: ifstream file1;
		 ofstream file;
		 string line;
public:
	void getdata();
	void writefile();
};
void OpenFile::getdata(){
	file1.open("test.txt");
	file.open("test1.txt");
	while(!file1.eof()){
		getline(file1, line);
	}
}
void OpenFile::writefile(){
		file<<line<<endl;
}
int main(){
	string line;
	OpenFile a;
	a.getdata();
	a.writefile();
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#12 muddturtle13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: File IO within a Class

Posted 13 September 2009 - 08:16 PM

Sorry for the delays in my replies. I have to do everything on the school's Linux server, so it takes a bit longer.

Anyways, this is my new code...

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct sStudent
{
	private:
		string studentID;
		string courseID;
		int hours;
		char grade;
};
		

class cStudent
{
	private:
		ifstream fileIn;
		ofstream fileOut;

	public:
		string studentID;
		string courseID;
		int hours;
		char grade;
		double gpa;
		bool end;

		cStudent(char []);
		~cStudent();
		void readFile();
		void writeFile(string, int, double);
};

cStudent::cStudent(char name[])
{
	fileIn.open(name);
	if(fileIn.fail())
	{
		cerr << "File " << name << " does not exist" << endl;
		abort();
	}
}

cStudent::~cStudent()
{
	fileIn.close();
}

void cStudent::readFile()
{
	fileIn.open("studentGrades.dat");

	fileIn >> studentID;
	end = fileIn.eof();

	while (!end)
	{
		cout << studentID;
		fileIn >> courseID;
		cout << courseID;
		fileIn >> hours;
		cout << hours;
		fileIn >> grade;
		cout << grade;		

		fileIn >> studentID;
		end = fileIn.eof();
	}
}

void cStudent::writeFile(string n, int h, double g)
{
	fileOut.open("studentSummary.dat");
	fileOut << n << endl;
	fileOut << h << endl;
	fileOut << g << endl;
}

int main()
{
	struct sStudent;
	class cStudent;

	char grade;
	double gpa;
	int hours;
	string studentID, courseID;

	cStudent.readFile();
	cStudent.writeFile(studentID, hours, gpa);

	return 0;
}



Obviously, it's still missing some code in the int main() part. That's where I plan on calculating each student's individual gpa. My question is about passing variables in and out of the functions. After reading in the input file's data, I need to also use it to calculate what I need to put in the output file. Does this make sense?
Was This Post Helpful? 0
  • +
  • -

#13 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: File IO within a Class

Posted 13 September 2009 - 08:22 PM

You mean that if a function alters the data and you need to send the new data?
If this is what you mean then just pass by reference '&'.

[EDIT]Your compiler didnt complained about this line end = fileIn.eof();?

This post has been edited by poncho4all: 13 September 2009 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#14 muddturtle13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: File IO within a Class

Posted 13 September 2009 - 08:27 PM

I'm trying to think of a good way to explain it...

I think it would be easiest if I did most of the work within a single function. The two most important functions I'm using are the readFile() and writeFile(). Rather than use reference parameters, can I just use writeFile() inside of readFile()?

In my while loop within the readFile(), I could use the writeFile() function and pass the variables into that before the loop restarts and replaces the studentID, course ID, hours, and grade. Make sense?
Was This Post Helpful? 0
  • +
  • -

#15 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: File IO within a Class

Posted 13 September 2009 - 08:31 PM

Yes that can be done
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

class OpenFile{
private: ifstream file1;
		 ofstream file;
		 string line;
public:
	void getdata();
	void writefile();
};
void OpenFile::getdata(){
	file1.open("test.txt");
	file.open("test1.txt");
	while(!file1.eof()){
		getline(file1, line);
		writefile();
	}
}
void OpenFile::writefile(){
		file<<line<<endl;
}
int main(){
	string line;
	OpenFile a;
	a.getdata();
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2