4

I cannot write data on a file with these pointer variables in the class. there is no error in the program but no data is written on the file. kindly someone tell me that where i am doing something wrong.

#include <iostream.h>
#include <fstream.h>

class studentinfo
{
    private:/*Creating Private Data Members */
        char* VUID;
        char* campusID;
        char* Studentname;
        char* Fathername;

    public:
        void Storefile();/* Function to Store Data in the File*/
        char Display();/*Function to Read and then Display Data from the File*/
        studentinfo(char*, char*, char*, char*);/*Constructor to initialize Data Members*/
        ~studentinfo();
};

/* Constructor Defined Here*/
studentinfo::studentinfo(char* VUID, char* campusID, char* Studentname, char* Fathername)
{
    cout << "Parameterized Contructor is Called" << endl << endl;
}

/*Destructor Defined Here*/
studentinfo::~studentinfo()
{
    cout << "Destructor Called for destruction of the object" << endl;
    system("pause");
}

/*Function to Store Data in the File Defined here*/
void studentinfo::Storefile()
{
    ofstream re;
    re.open("record.txt");
    if(!re)/*Error Checking Mechanism*/
    {
        cout<<"Error Reading File"<<endl;
    }

    re << VUID << endl << campusID << endl << Studentname << endl << Fathername << endl;/*Using data members to Store data in the File*/
    cout << "All the Data Members are Stored in a File" << endl << endl;
    re.close();
}

/*Function to Read and then Display the data in the File is definde here */              
char studentinfo::Display()
{
    char output[100];/*Array to store and display the data*/
    ifstream reh;
    reh.open("record.txt");
    if(!reh)
    {
        cout << "Error Reading File" << endl;
    }

    cout << "Following is My Data" << endl << endl;
    while(!reh.eof()){
        reh.getline(output, 100, '\n');/*Reading the data and storing it in the 'output' array line by line*/
        cout << output << endl;
    }

    reh.close();
}


/*Main Function starting here*/                  
main()
{
    studentinfo s1("mc130202398", "PMTN08", "Rehan Shahzad Siddiqui","Rizwan Ali Siddiqui");/*Object Created and Initialized by constructor calling*/
    s1.Storefile();/*Function Call*/
    s1.Display();/*Function Call*/

    system("pause");
}
Talvalin
  • 7,789
  • 2
  • 30
  • 40
Sireiz
  • 383
  • 3
  • 10

2 Answers2

6

Your constructor is broken and leaves all the pointers unassigned. You can't use a variable's value until you assign it one.

Also, what crappy compiler are you using or what warnings settings do you have? Your constructor is being passed pointers to constants but it takes non-const pointers. That should definitely have caused a warning, pointing to your mishandling of these pointers.

  studentinfo s1("mc130202398", "PMTN08", "Rehan Shahzad Siddiqui","Rizwan Ali Siddiqui");/*Object Created and Initialized by constructor calling*/

Notice you pass the constructor a bunch of constants.

studentinfo::studentinfo(char* VUID, char* campusID, char* Studentname, char* Fathername)

Oops, but the constructor takes regular char* pointers. So what are these pointers supposed to point to?

Tip: Use sensible C++ classes like std::string and these problems will magically go away.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • so what to do now? i am really in the initial stages. kindly help me. can you make some changes to his code to make it work? plz. – Sireiz Jun 26 '13 at 08:22
  • @Sireiz: I would just change the members from `char*` to `std::string`. Also, you have to actually set their values in the constructor. Lastly, don't use the same name for two different things. For example, you have a class member called "VUID" and a constructor parameter called "VUID". Don't do that. (Some people like to use "mVUID" or "_VUID" for the member variable. Some use "vuid" for the parameter.) – David Schwartz Jun 26 '13 at 08:24
  • how to actually set the values? – Sireiz Jun 26 '13 at 08:27
  • David I really need that. plz. – Sireiz Jun 26 '13 at 08:31
  • In order to understand the problem, note that for example the `VUID` name in the parameter list of the definition of the constructor and the `VUID` name of the member variable of the class are in different scopes and refere to different variables. Their common name might have lead you to believe that there was indeed an assignment happening in that constructor call. – Philipp Matthias Schäfer Jun 26 '13 at 08:32
  • By changing the name either in the class or the constructor, then using `x = y;` to set member variable `x` to the value of `y`. – Mats Petersson Jun 26 '13 at 08:40
1

A rewrite that addresses a number of points:

Let me know if you have any further questions.

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

class studentinfo
{
    private:/*Creating Private Data Members */
        std::string m_VUID;
        std::string m_campusID;
        std::string m_Studentname;
        std::string m_Fathername;

    public:
        void Storefile();/* Function to Store Data in the File*/
        void Display();/*Function to Read and then Display Data from the File*/
        studentinfo(std::string, std::string, std::string, std::string);/*Constructor to initialize Data Members*/
        ~studentinfo();
};

/* Constructor Defined Here*/
studentinfo::studentinfo(std::string VUID, std::string campusID, std::string Studentname, std::string Fathername)
: m_VUID(VUID)
, m_campusID(campusID)
, m_Studentname(Studentname)
, m_Fathername(Fathername)
{
    std::cout << "Parameterized Contructor is Called" << std::endl << std::endl;
}

/*Destructor Defined Here*/
studentinfo::~studentinfo()
{
    std::cout << "Destructor Called for destruction of the object" << std::endl;
}

/*Function to Store Data in the File Defined here*/
void studentinfo::Storefile()
{
    std::ofstream re;
    re.open("record.txt");
    if(!re)/*Error Checking Mechanism*/
    {
        std::cout << "Error opening file" << std::endl;
    }

    // Using data members to store data in the file
    re << m_VUID.c_str() << std::endl; 
    re << m_campusID.c_str() << std::endl; 
    re << m_Studentname.c_str() << std::endl;
    re << m_Fathername.c_str() << std::endl; 

    std::cout << "All the data members are stored in a file" << std::endl << std::endl;
    re.close();
}

/* Function to read and then display the data in the file is defined here */
void studentinfo::Display()
{
    std::string in;/*Array to store and display the data*/
    std::ifstream reh("record.txt");
    if(!reh)
    {
        std::cout << "Error Reading File" << std::endl;
    }

    std::cout << "Following is My Data" << std::endl << std::endl;
    while(std::getline(reh, in))
    {
        std::cout << in << std::endl;
    }

    reh.close();
}


/* Main Function starts here*/
void main()
{
    // Object created and initialised by calling constructor
    studentinfo s1("mc130202398", "PMTN08", "Rehan Shahzad Siddiqui","Rizwan Ali Siddiqui"); 

    s1.Storefile(); /*Function Call*/
    s1.Display(); /*Function Call*/

    system("pause");
}
Community
  • 1
  • 1
Talvalin
  • 7,789
  • 2
  • 30
  • 40
  • thank u very much @talvalin, i searched for string copy functions and found assign function so i used it. studentinfo::studentinfo(string vUID, string CampusID, string studentname, string fathername) { cout<<"Parameterized Contructor is Called"< – Sireiz Jun 26 '13 at 15:49
  • Er, I would just use the code as posted in my answer, otherwise just do straight assignment. eg: `VUID = vUID;` – Talvalin Jun 26 '13 at 16:12
  • Again thank you very much for your help, i completed my assignment in time. :) i am a newbie in programming and we are learning step by step, we started from 'c' and now coming through to c++. i don't know much about various string functions or assignments otherwise i would have used the simple assignment. – Sireiz Jun 26 '13 at 17:33
  • I'm glad to hear that. Please mark this or David Schwartz's answer as the accepted one and upvote both if they were helpful. :) – Talvalin Jun 26 '13 at 22:23
  • I tried to do that before you asked but i am not eligible yet to do that. :) – Sireiz Jun 27 '13 at 05:23