【问题标题】:Cannot write the data in the file, no error in the program C++无法将数据写入文件,程序C++中没有错误
【发布时间】:2013-06-23 06:19:03
【问题描述】:

我无法在类中使用这些指针变量将数据写入文件。程序没有错误,但文件上没有写入数据。 请有人告诉我我做错了什么。

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

【问题讨论】:

  • 只是提到您应该将错误消息路由到stderr而不是stdout

标签: c++ pointers file-handling


【解决方案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");
}

【讨论】:

  • 非常感谢@talvalin,我搜索了字符串复制函数并找到了分配函数,所以我使用了它。 studentinfo::studentinfo(string vUID, string CampusID, string studentname, string Fathername) { cout
  • 呃,我只会使用我的答案中发布的代码,否则就直接分配。例如:VUID = vUID;
  • 再次非常感谢您的帮助,我及时完成了作业。 :) 我是编程新手,我们正在逐步学习,我们从“c”开始,现在进入 c++。我不太了解各种字符串函数或赋值,否则我会使用简单的赋值。
  • 我很高兴听到这个消息。请将此或 David Schwartz 的答案标记为已接受的答案,如果有帮助,请点赞。 :)
  • 我在你问之前就尝试过这样做,但我还没有资格这样做。 :)
【解决方案2】:

您的构造函数已损坏,并且所有指针都未分配。在为变量赋值之前,您不能使用变量的值。

另外,你使用的是什么蹩脚的编译器,或者你有什么警告设置?您的构造函数正在传递指向常量的指针,但它需要非常量指针。这应该肯定引起警告,指出您对这些指针的处理不当。

  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* 指针。那么这些指针应该指向什么?

提示:使用像 std::string 这样合理的 C++ 类,这些问题就会神奇地消失。

【讨论】:

  • 那么现在该怎么办?我真的处于初级阶段。请帮助我。您可以对他的代码进行一些更改以使其正常工作吗?请。
  • @Sireiz:我只想将成员从char* 更改为std::string。此外,您必须在构造函数中实际设置它们的值。最后,不要为两个不同的事物使用相同的名称。例如,您有一个名为“VUID”的类成员和一个名为“VUID”的构造函数参数。不要那样做。 (有些人喜欢使用“mVUID”或“_VUID”作为成员变量。有些人使用“vuid”作为参数。)
  • 如何实际设置值?
  • 大卫我真的需要那个。请。
  • 为了理解问题,注意例如构造函数定义的参数列表中的VUID名称和类的成员变量VUID名称是不同的范围并引用不同的变量。它们的通用名称可能会让您相信在构造函数调用中确实发生了赋值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多