2013-06-26 38 views
4

我無法在類中使用這些指針變量寫入數據。程序中沒有錯誤,但沒有數據寫入文件。 請親切地告訴我,我在哪裏做錯了什麼。無法在文件中寫入數據,程序中沒有錯誤C++

#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"); 
} 
+0

只要提及您應該將錯誤消息路由到stderr而不是stdout – rkosegi

回答

1

,解決了一些點的改寫:

讓我知道,如果您有任何進一步的問題。

#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"); 
} 
+0

非常感謝你@talvalin,我搜索字符串複製功能,並找到分配功能,所以我用它。 學生信息:: studentinfo(字符串vUID,字符串CampusID,字符串學生名,字符串父名) { cout <<「參數化構造函數調用」<< endl << endl; (*)分配給數據成員的值*/ VUID.assign(vUID);/*爲數據成員*/ 分配值campusID.assign(CampusID);/*爲數據成員分配值*/ Studentname.assign(studentname);// Fathername.assign(fathername);/*賦值給數據成員*/ } – Sireiz

+0

它是怎麼回事?這夠好嗎? – Sireiz

+0

呃,我只是使用我的答案張貼的代碼,否則只是做直接任務。例如:'VUID = vUID;' – Talvalin

6

你的構造函數壞了,所有的指針都沒有指定。在分配一個變量之前,您不能使用變量的值。

另外,你使用的是什麼樣的編譯器,或者你有什麼警告設置?您的構造函數正在傳遞指向常量的指針,但它需要非常量指針。那應該肯定已經引起了一個警告,指出你對這些指針的處理不當。

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

請注意,您傳遞構造函數的一堆常量。

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

糟糕,但構造函數需要定期的char*指針。那麼這些指針應該指向什麼呢?

提示:使用明智的C++類,如std::string,這些問題會神奇地消失。

+0

那麼現在該怎麼辦?我真的處於最初階段。請幫助我。你可以對他的代碼進行一些更改以使其工作嗎? PLZ。 – Sireiz

+1

我正在使用devC++ 4.9.9.2 – Sireiz

+0

@Sireiz:我只是將成員從'char *'更改爲'std :: string'。另外,您必須在構造函數中實際設置它們的值。最後,不要爲兩個不同的事物使用相同的名稱。例如,您有一個名爲「VUID」的類成員和一個名爲「VUID」的構造函數參數。不要這樣做。 (有些人喜歡使用「mVUID」或「_VUID」作爲成員變量,有些人使用「vuid」作爲參數。) –

相關問題