c++ 了解文件处理I/O

hiz5n14c  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(162)

我用两种方法写了一个简单的程序。这个程序从用户那里获取数据,将数据存储在txt文件中,检索数据并显示出来。问题是一种方法有效,而另一种方法无效。我不明白为什么。
有效的方法是这样的:

#include <iostream>
#include <fstream>

using namespace std;

class customer{
    public:

    // Declaring member variables and functions
    string name, address, telNo;
    int age;
    void createCustomer();
    void displayInfo(string inputName);
};

// Member function of customer to enter new customer details
void customer::createCustomer(){
    customer c;
    ofstream file;
    file.open("customers.txt", ios::out);
    cout << "Enter Name: ";
    cin >> c.name;
    cout << "Enter Age: ";
    cin >> c.age;
    cout << "Enter Address: ";
    cin >> c.address;
    cout << "Enter Telephone Number: ";
    cin >> c.telNo;
    file.write((char*)&c, sizeof(c));
    file.close();
    cout << "\n";
}

// Member function of customer to display customer information
void customer::displayInfo(string inputName){
    customer c;
    ifstream file;
    file.open("customers.txt", ios::in);
    if(!file){
        cout << "Could Not Open customers.txt File\n";
        return;
    }
while(!file.eof()){
file.read((char*)&c, sizeof(c));
if (inputName == c.name){
    cout << "\n";
    cout << "Name-------------> " << c.name << endl;
    cout << "Age--------------> " << c.age << endl;
    cout << "Telephone Number-> " << c.telNo << endl;
    cout << "Address----------> " << c.address << endl;
    cout << "\n";
    break;
    }
}
    file.close();
}

int main(){
    customer c;
    c.createCustomer();
    c.displayInfo("name");
    return 0;
}

它给出以下输出:
输入名称:名称
输入年龄:21
输入地址:加
输入电话号码:泰尔诺
名称------------〉名称
年龄-------------〉21岁
电话号码-〉telno
地址----------〉添加
不起作用的是:

#include <iostream>
#include <fstream>

using namespace std;

class customer{
    public:

    // Declaring member variables and functions
    string name, address, telNo;
    int age;
    void createCustomer();
    void displayInfo();
};

// Member function of customer to enter new customer details
void customer::createCustomer(){
    customer c;
    ofstream file;
    file.open("customers.txt", ios::out);
    cout << "Enter Name: ";
    cin >> c.name;
    cout << "Enter Age: ";
    cin >> c.age;
    cout << "Enter Address: ";
    cin >> c.address;
    cout << "Enter Telephone Number: ";
    cin >> c.telNo;
    file.write((char*)&c, sizeof(c));
    file.close();
    cout << "\n";
}

// Member function of customer to display customer information
void customer::displayInfo(){
    string inputName = "name";
    customer c;
    ifstream file;
    file.open("customers.txt", ios::in);
    if(!file){
        cout << "Could Not Open customers.txt File\n";
        return;
    }
    while(!file.eof()){
        file.read((char*)&c, sizeof(c));
        if (inputName == c.name){
            cout << "\n";
            cout << "Name-------------> " << c.name << endl;
            cout << "Age--------------> " << c.age << endl;
            cout << "Telephone Number-> " << c.telNo << endl;
            cout << "Address----------> " << c.address << endl;
            cout << "\n";
            break;
        }
    }
    file.close();
}

int main(){
    customer c;
    c.createCustomer();
    c.displayInfo();
    return 0;
}

我所做的只是将字符串inputName变量放入函数中,而不是将其作为参数传递。
它只提供以下输出:
输入名称:名称
输入年龄:21
输入地址:加
输入电话号码:泰尔诺
如果删除if条件:

while(!file.eof()){
    file.read((char*)&c, sizeof(c));
    //if (inputName == c.name){
        cout << "\n";
        cout << "Name-------------> " << c.name << endl;
        cout << "Age--------------> " << c.age << endl;
        cout << "Telephone Number-> " << c.telNo << endl;
        cout << "Address----------> " << c.address << endl;
        cout << "\n";
        break;
    //}
}

我得到以下输出:
输入名称:名称
输入年龄:21
输入地址:加
输入电话号码:泰尔诺
名称-----------〉0
年龄-------------〉21岁
电话号码-〉姓名
地址--------〉§
为什么姓名和地址字段输出随机符号,而电话号码字段输出姓名字段的值?

ulydmbyx

ulydmbyx1#

这两个程序都不正确。在customer类型上使用read是错误的,因为它包含需要构造的子对象,即string成员nameaddresstelNo。调用read不会调用您正在阅读的对象的任何构造函数,因此这是无效的。
由于read不适用于字符串,因此在包含string的类上使用write也没有任何意义,因为您将无法回读所编写的内容。
如果你想把一个customer写入一个文件,你可以这样做

file << c.name << ' ' << c.age << ' ' << c.telNo << ' ' << c.address << '\n';

然后是类似的阅读(虽然你然后你必须小心你的数据中的任何空格)。

相关问题