I did this program last night and ran it. It worked alright and gave the required output. But now I return in the morning and it isn't working!
Basically it is supposed to create a file named record.txt and populate it with the given info. then read the file and display the info back on the screen. It did that last night but I can't get it to work today. I'm using g++ and couldn't understand what went wrong. I never altered the program. Its the same one from last night so if any one can point out what should I do, I'd be great ful. Here's the code
Code:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
/**************************
** Class Decleration **
***************************/
class StudentInfo
{
char* VUID;
char* campusID;
char* Studentname;
char* Fathername;
public:
StudentInfo(char* vu_Id, char* campus_Id, char* student_Name, char* father_Name)
{
cout << "Parameterized constructor is called." << endl;
setVUID(vu_Id);
setcampusID(campus_Id);
setStudentname(student_Name);
setFathername(father_Name);
}
void setVUID(char* vu_Id)
{
VUID = new char[strlen(vu_Id) + 1];
strcpy(VUID, vu_Id);
}
void setcampusID(char* campus_Id)
{
campusID = new char[strlen(campus_Id) + 1];
strcpy(campusID, campus_Id);
}
void setStudentname(char* student_Name)
{
Studentname = new char[strlen(student_Name) + 1];
strcpy(Studentname, student_Name);
}
void setFathername(char* father_Name)
{
Fathername = new char[strlen(father_Name) + 1];
strcpy(Fathername, father_Name);
}
void Storefile()
{
cout << "The File has been saved as <record.txt>" << endl;
ofstream outFile;
char* outputFileName = "record.txt";
outFile.open(outputFileName, ios::out);
if(!outFile)
{
cout << "\n Unable to open file." << endl;
}
else
{
outFile << VUID;
outFile << endl;
outFile << campusID;
outFile << endl;
outFile << Studentname;
outFile << endl;
outFile << Fathername;
}
}
void Display()
{
ifstream inputStream;
char* fileName = "record.txt";
inputStream.open(fileName);
if(!inputStream)
{
cout << "\nCan't Open the file for read";
}
else
{
char line[100];
inputStream.getline(line, 100);
setVUID(line);
inputStream.getline(line, 100);
setcampusID(line);
inputStream.getline(line, 100);
setStudentname(line);
inputStream.getline(line, 100);
setFathername(line);
cout << "The Data Stored is :" << endl;
cout << VUID << endl;
cout << campusID << endl;
cout << Studentname << endl;
cout << Fathername;
cout << endl;
}
}
}
/***************************
* The Program Begains Here**
****************************/
int main()
{
StudentInfo studentInfo("mc130202735", "Bqta02", "Nabeel Abbas", "Sammar abbas");
studentInfo.Storefile();
studentInfo.Display();
return 0;
}