Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot write the data in the file, no error in the program C++

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");
}
like image 681
Sireiz Avatar asked Mar 24 '23 10:03

Sireiz


1 Answers

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.

like image 64
David Schwartz Avatar answered Apr 06 '23 20:04

David Schwartz