13 Replies - 507 Views - Last Post: 07 August 2012 - 05:54 PM Rate Topic: -----

#1 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Question: How to read from a file and print only NO option.

Posted 06 August 2012 - 09:17 PM

Just trying to read from a file to see if any of the students aren't enrolled in any courses.
I know how to open the file etc, just trying to figure out how to pick up one block from the student file I have attached and read the N only. Here's my code so far, thanks.

//---------------------------------------------------------------------------
#include <vcl.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

const char STUDENT[] = "C:\\students";
const char COURSES[] = "C:\\courses";

class Student
{
	char first_name[ 16 ];
	char second_name[ 16 ];
	int id_number;
	char enrolled;
};

class Course
{
	int course_identifier;
	char course_name[ 32 ];
	int credits;
	int id_number;
};

fstream MyFile1( STUDENT , ios :: binary | ios :: in | ios :: out );
fstream MyFile2( COURSES , ios :: binary | ios :: in | ios :: out );
//---------------------------------------------------------------------------

int _tmain(int argc, _TCHAR* argv[])
{
	if ( ! MyFile1 )
	{
		fstream MyFile1( STUDENT , ios :: binary | ios :: out );
	}

	MyFile1.close();
	MyFile1.open( STUDENT , ios :: binary | ios :: in | ios :: out );

	if ( ! MyFile1 )
	{
		cout << "File Error";
		getch();
		return 1;
	}

	if ( ! MyFile2 )
	{
		fstream MyFile2( COURSES , ios :: binary | ios :: out );
	}

	MyFile2.close();
	MyFile2.open( COURSES , ios :: binary | ios :: in | ios :: out );

	if ( ! MyFile2 )
	{
		cout << "File Error";
		getch();
		return 1;
	}

	cout << "************************" << endl;
	cout << "* 159.234 Assignment 2 *" << endl;
	cout << "*   05155932 Syms C.   *" << endl;
	cout << "*       S2 2012        *" << endl;
	cout << "************************" << endl << endl;

	cout << "Students not enrolled in any paper: " << endl;


	getch();
	return 0;
}
//---------------------------------------------------------------------------



Output should look something like:

Students not enrolled in any paper:
Blake, James: ID 7177323, paid fees N

Cheers.

Attached File(s)


This post has been edited by rdhc1330: 06 August 2012 - 09:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question: How to read from a file and print only NO option.

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Question: How to read from a file and print only NO option.

Posted 06 August 2012 - 10:20 PM

Quote

I know how to open the file etc,

Really? Then I have a couple of questions. One why are you opening your file twice:
	if ( ! MyFile1 )
	{
		fstream MyFile1( STUDENT , ios :: binary | ios :: out );
	}

	MyFile1.close();
	MyFile1.open( STUDENT , ios :: binary | ios :: in | ios :: out );

	if ( ! MyFile1 )

For one thing this code should not compile, in the first line above MyFile1 has not been declared, you don't declare this variable until inside the if statement. Next the same thing with the third line, you try to close a file that has already been closed because the variable is destroyed when it leaves the if statement. And since that variable has been destroyed when leaving the block of the if statement it no longer exists for the rest of your code. You need to remove the first if statement, the close statement and the open statement. Your fstream MyFile1( STUDENT , ios :: binary | ios :: out ); line is creating an instance of the fstream, and trying to open the file. So you don't need the open statement. Next this file will only be opened if the file already exists, because a fstream will not create a file if it doesn't exist unless you use either the app, or trunc open mode.

Next your file looks like:

Quote

5
Charles
Syms
5155932
Y

This is not a binary file, it is a text file, you don't want the ios::binary open mode. I recommend that unless you plan to read and write to this file that you use either ifstream for reading, or ofstream for writing.

You may want to study this link: Basic file IO in C++

Jim
Was This Post Helpful? 0
  • +
  • -

#3 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 02:20 AM

Thanks for your input Jim.
First off the file is supposed to be binary but when I tried to upload the file it said that it isn't a format that is allowed to be uploaded so I changed it to txt. But I understand where you're coming from and I can probably just leave it as a txt file. I don't need to write to the file you're right, I just need to open and read from it to locate the N and then print the information associated with it. This is where I'm confused. Thanks I've read that file and changed my code to this:

//---------------------------------------------------------------------------
#include <vcl.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

const char STUDENT[] = "C:\\students";
const char COURSES[] = "C:\\courses";

class Student
{
	char first_name[ 16 ];
	char second_name[ 16 ];
	int id_number;
	char enrolled;
};

class Course
{
	int course_identifier;
	char course_name[ 32 ];
	int credits;
	int id_number;
};
//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
	ifstream MyFile1( STUDENT , ios :: out );
	ifstream MyFile2( COURSES , ios :: out );

	if ( ! MyFile1 )
	{
		cout << "File Error";
		getch();
		return 1;
	}

	if ( ! MyFile2 )
	{
		cout << "File Error";
		getch();
		return 1;
	}

	cout << "************************" << endl;
	cout << "* 159.234 Assignment 2 *" << endl;
	cout << "*   05155932 Syms C.   *" << endl;
	cout << "*       S2 2012        *" << endl;
	cout << "************************" << endl << endl;

	cout << "Students not enrolled in any paper: " << endl;


	getch();
	return 0;
}
//---------------------------------------------------------------------------


This post has been edited by rdhc1330: 07 August 2012 - 02:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 02:58 AM

Read the link that Jim provided! Here's a tutorial too

This:

ifstream MyFile1( STUDENT , ios :: out );
is opening the file for WRITING, not READING.

So far it's like you've not even tried to give it a go.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 06:07 AM

Quote

First off the file is supposed to be binary but when I tried to upload the file it said that it isn't a format that is allowed to be uploaded so I changed it to txt.

What do you mean by you changed it to txt? If you just added the .txt extension and didn't rewrite the file then this file is a plain text file. A binary file is not readable by programs such as Notepad, Wordpad, etc. Also C/C++ don't care what your file is named, or what it's extension is, the name and extension could be anything.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 04:41 PM

Hey sorry, I did read that and didn't change something by accident.
I remembered about classes and changed the code up a bit to include the classes and everything seems to be working right now. Now I just need to add in the search function and print only the info that is associated with the "N" for enrolment.

//---------------------------------------------------------------------------
#include <vcl.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

const char STUDENT[] = "C:\\students";
const char COURSES[] = "C:\\courses";

class Student{
private:
	char first_name[ 16 ];
	char second_name[ 16 ];
	int id_number;
	char enrolled;
public:
	void checkFile();
};

class Course{
private:
	int course_identifier;
	char course_name[ 32 ];
	int credits;
	int id_number;
public:
	void checkFile();
};

ofstream MyFile1( STUDENT , ios :: in );
ofstream MyFile2( COURSES , ios :: in );

void Student :: checkFile()
{
	if ( ! MyFile1 )
	{
		cout << "File Error";
		getch();
	}
}

void Course :: checkFile()
{
	if ( ! MyFile2 )
	{
		cout << "File Error";
		getch();
	}
}
//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{


	cout << "************************" << endl;
	cout << "* 159.234 Assignment 2 *" << endl;
	cout << "*   05155932 Syms C.   *" << endl;
	cout << "*       S2 2012        *" << endl;
	cout << "************************" << endl << endl;

	cout << "Students not enrolled in any paper: " << endl;


	getch();
	return 0;
}
//---------------------------------------------------------------------------



I think what I've done to date now is right. Any help would be appreciated on the search function, I have written something up like this in the past but nothing to do with classes. THanks
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 04:47 PM

Quote

I think what I've done to date now is right.

Not really. Why are you opening an output stream for input?
ofstream MyFile1( STUDENT , ios :: in );

An ofstream is for output, an ifstream is for input. Plus these lines should be inside a function, not global. You need to restudy the links provided in post #2 and post #4,


Quote

Any help would be appreciated on the search function,

Before you worry about a search function you may want to try reading your files and placing the contents into variables.

Jim

This post has been edited by jimblumberg: 07 August 2012 - 04:48 PM

Was This Post Helpful? 1
  • +
  • -

#8 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 05:25 PM

OK so after a class today I've had a real go at it and think I'm in the right region. I've managed to make all the records print and I think my classes look a little more like classes. However I'm just trying to print that record with enrolled = "N" and I can't seem to figure it out. Could maybe on the printAll function enter an if statement but everything I try comes back with errors.

//---------------------------------------------------------------------------
#include <vcl.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;

const char STUDENT[] = "C:\\students.txt";
const char COURSES[] = "C:\\courses";

class Student
{
public:
	Student();
	Student(char firstName[] , char secondName[] , int id , char enrld);
	~Student();
	int getIDNumber();
	char* getFirstName();
	char* getSecondName();
	char getEnrolled();
	void setIDNumber( int id );
	void setFirstName( char firstName[] );
	void setSecondName( char secondName[] );
	void setEnrolled( char enrolled );
	void print();
private:
	char first_name[ 16 ];
	char second_name[ 16 ];
	int id_number;
	char enrolled;
};

class Course
{
public:
	int course_identifier;
	char course_name[ 32 ];
	int credits;
	int id_number;
};

Student :: Student()
{

}

Student :: ~Student()
{

}

Student :: Student( char firstName[], char secondName[], int id, char enrld )
{
	strcpy( first_name , firstName );
	strcpy(second_name , secondName );
	id_number = id;
	enrolled = enrld;
}

int Student :: getIDNumber()
{
	return id_number;
}

char Student :: getEnrolled()
{
	return enrolled;
}

char* Student :: getFirstName()
{
	return first_name;
}

char* Student :: getSecondName()
{
	return second_name;
}

void Student :: setIDNumber( int id )
{
	id_number = id;
}

void Student :: setEnrolled( char enrolled )
{
	this -> enrolled = enrolled;
}

void Student :: setFirstName( char firstName[] )
{
	strcpy( first_name , firstName );
}

void Student :: setSecondName( char secondName[] )
{
	strcpy( second_name , secondName );
}

void Student :: print()
{
	cout << "First Name: " << first_name << endl;
	cout << "Second Name: " << second_name << endl;
	cout << "ID: " << id_number << endl;
	cout << "Enrolled: " << enrolled << endl;

	cout << endl;
}

bool readStudents( Student[] , int& );
void printAllStudents( Student[], int );
//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
	Student students[ 1024 ];
	int nrOfStudents = 0;

	if (!readStudents(students, nrOfStudents))
		return 1;

	printAllStudents(students,nrOfStudents);

	getch();
	return 0;
}
//---------------------------------------------------------------------------
bool readStudents( Student students[] , int &nrOfStudents)
{
	ifstream read;
	read.open(STUDENT);

	if (! read.is_open())
	{
		return false;
	}

	int nr = 0;

	char fName[16];
	char sName[16];
	char enrolled;
	int id;

	read >> nr;
	while (! read.eof() && nrOfStudents != nr)
	{
		read >> fName;
		read >> sName;
		read >> id;
		read >> enrolled;

		students[ nrOfStudents ] = Student( fName , sName , id , enrolled );

		nrOfStudents++;
	}

	read.close();
	return true;
}
//---------------------------------------------------------------------------
void printAllStudents( Student students[] , int nrOfStudents )
{
	for (int i = 0; i < nrOfStudents; i++)
	{
		students[i].print();
	}
}
//---------------------------------------------------------------------------


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 05:33 PM

Quote

However I'm just trying to print that record with enrolled = "N" and I can't seem to figure it out. Could maybe on the printAll function enter an if statement but everything I try comes back with errors.

Show us the code where you tried, and then post the complete error messages exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,420
  • Joined: 05-May 12

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 05:35 PM

View Postrdhc1330, on 07 August 2012 - 05:25 PM, said:

However I'm just trying to print that record with enrolled = "N" and I can't seem to figure it out. Could maybe on the printAll function enter an if statement but everything I try comes back with errors.

What have you tried? What errors are you getting?
Was This Post Helpful? 0
  • +
  • -

#11 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 05:36 PM

[quote name='rdhc1330' date='07 August 2012 - 05:25 PM' timestamp='1344385550' post='1680461']

void Student :: print()
{
	cout << "First Name: " << first_name << endl;
	cout << "Second Name: " << second_name << endl;
	cout << "ID: " << id_number << endl;
	cout << "Enrolled: " << enrolled << endl;

	cout << endl;
}



Has been changed to:

void Student :: print()
{
	if( enrolled == 'N' )
	{
		cout << "\t" << second_name << ", " << first_name << ": ID "
		<< id_number << ", paid fees " << enrolled;
	}
	cout << endl;
}


This post has been edited by rdhc1330: 07 August 2012 - 05:37 PM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 05:41 PM

Please show a small sample of your input file.

Quote

Has been changed to:

And is this working or not? If not what is wrong. Be specific.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 05:49 PM

Sorry Jim! OK so that part of the code is working fine now... Here's the code in its entirety:

//---------------------------------------------------------------------------
#include <vcl.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;

const char STUDENT[] = "C:\\students.txt";
const char COURSES[] = "C:\\courses.txt";

class Student
{
public:
	Student();
	Student(char firstName[] , char secondName[] , int id , char enrld);
	~Student();
	int getIDNumber();
	char* getFirstName();
	char* getSecondName();
	char getEnrolled();
	void setIDNumber( int id );
	void setFirstName( char firstName[] );
	void setSecondName( char secondName[] );
	void setEnrolled( char enrolled );
	void print();
private:
	char first_name[ 16 ];
	char second_name[ 16 ];
	int id_number;
	char enrolled;
};

class Course
{
public:
	int course_identifier;
	char course_name[ 32 ];
	int credits;
	int id_number;
};

Student :: Student()
{

}

Student :: ~Student()
{

}

Student :: Student( char firstName[], char secondName[], int id, char enrld )
{
	strcpy( first_name , firstName );
	strcpy(second_name , secondName );
	id_number = id;
	enrolled = enrld;
}

int Student :: getIDNumber()
{
	return id_number;
}

char Student :: getEnrolled()
{
	return enrolled;
}

char* Student :: getFirstName()
{
	return first_name;
}

char* Student :: getSecondName()
{
	return second_name;
}

void Student :: setIDNumber( int id )
{
	id_number = id;
}

void Student :: setEnrolled( char enrolled )
{
	this -> enrolled = enrolled;
}

void Student :: setFirstName( char firstName[] )
{
	strcpy( first_name , firstName );
}

void Student :: setSecondName( char secondName[] )
{
	strcpy( second_name , secondName );
}

void Student :: print()
{
	if( enrolled == 'N' )
	{
		cout << "   " << second_name << ", " << first_name << ": ID "
		<< id_number << ", paid fees " << enrolled;
	}
}

bool readStudents( Student[] , int& );
void printAllStudents( Student[], int );
//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
	Student students[ 1024 ];
	int nrOfStudents = 0;

	cout << "************************" << endl;
	cout << "* 159.234 Assignment 2 *" << endl;
	cout << "*   05155932 Syms C.   *" << endl;
	cout << "*       S2 2012        *" << endl;
	cout << "************************" << endl << endl;

	cout << "Students not enrolled in any paper: " << endl;

	if( !readStudents ( students , nrOfStudents ) )
		return 1;

	printAllStudents( students , nrOfStudents );

	getch();
	return 0;
}
//---------------------------------------------------------------------------
bool readStudents( Student students[] , int &nrOfStudents)
{
	ifstream read;
	read.open(STUDENT);

	if (! read.is_open())
	{
		return false;
	}

	int nr = 0;

	char fName[16];
	char sName[16];
	char enrolled;
	int id;

	read >> nr;
	while (! read.eof() && nrOfStudents != nr)
	{
		read >> fName;
		read >> sName;
		read >> id;
		read >> enrolled;

		students[ nrOfStudents ] = Student( fName , sName , id , enrolled );

		nrOfStudents++;
	}

	read.close();
	return true;
}
//---------------------------------------------------------------------------
void printAllStudents( Student students[] , int nrOfStudents )
{
	for (int i = 0; i < nrOfStudents; i++)
	{
		students[i].print();
	}
}
//---------------------------------------------------------------------------



The required output is supposed to be like:

Quote

Students not enrolled in any paper:
Blake, James: ID 7177323, paid fees N


It is doing this now after I changed the above piece of code to include the if statement.

Now I just need the code to read through the courses text file and read to print this:

Quote

Courses with no students enrolled: 157314, System-Analysis, credits 12, 0 students


I think I'm on the right track here, my code feels messy but it's the first time I've had a real go at classes so if there's anything else you could recommend then that would be fantastico.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,420
  • Joined: 05-May 12

Re: Question: How to read from a file and print only NO option.

Posted 07 August 2012 - 05:54 PM

Seems to work for me. Here's the output using the students.txt from post #1.

        Blake, James: ID 7177323, paid fees N






Since the forum software was so kind to trim white space, there is 1 blank line before the output string, and 3 blank lines after it.

Since your objective was print out the student who had not paid any fees, it looks like you've succeeded.

This post has been edited by Skydiver: 07 August 2012 - 05:56 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1