简体   繁体   English

在文件结构中使用read()和write()使用字符串代替char数组

[英]Using string instead of char array with read() and write() in a struct to file

Is there a way a string could be used instead of a char[] array in the below struct , and still be saved to a file using read and write functions without getting a runtime error? 有没有办法一个string可以用来代替char[]在下面的阵列struct ,而且还可以保存到使用文件readwrite的功能没有得到一个运行时错误?

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

struct details
{
    char name[30];
    int add_year;
};

const char * file = "students.dat";

int main()
{
    details x;
    ifstream ifile;
    ifile.open(file,ios_base::in|ios_base::binary);
    if(ifile.is_open())
    {
        cout<<"Contents of "<<file<<" is"<<endl;
        while(ifile.read((char *)&x,sizeof x))
        {
            cout<<x.name<<" - "<<x.add_year<<endl;
        }
        ifile.close();
    }
    ofstream ofile(file,ios_base::out|ios_base::app|ios_base::binary);
    if(!ofile.is_open())
    {
        cerr<<"Can't open "<<file<<endl;
        exit(EXIT_FAILURE);
    }
    cout<<"Name"<<endl;
    cin.get(x.name,30);
    cout<<"Year added"<<endl;
    cin>>x.add_year;
    ofile.write((char *) &x, sizeof x);
    ofile.close();
    cin.get();
    return 0;
}

A std::string (or any other dynamic sized container) contains a pointer to its character data that is stored elsewhere in memory. std::string (或任何其他动态大小的容器)包含指向其字符数据的指针,该指针存储在内存中的其他位置。 If you were to read() / write() the std::string itself as-is, you would only be reading/writing the pointer value, not the actual character data. 如果std::string原样read() / write() std::string本身,则只会读取/写入指针值,而不是实际的字符数据。 This is not the case with a fixed array. 固定数组不是这种情况。

In order to use std::string in your struct , you would have to (de)serialize the content of the struct back and forth so you can take dynamic data into account. 为了在struct使用std::string ,您必须前后来反序列化struct的内容,以便可以将动态数据考虑在内。

For example: 例如:

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

using namespace std;

struct details
{
    string name;
    int32_t add_year;
};

istream& operator>>(istream &in, details &out)
{
    int32_t len;
    if (in.read((char*) &len, sizeof(len)))
    {
        out.name.resize(len);
        if (len > 0) in.read(&(out.name[0]), len);
        in.read((char*) &(out.add_year), sizeof(out.add_year));
    }
    return in;
}

ostream& operator<<(ostream &out, const details &in)
{
    int32_t len = in.name.size();
    out.write((char*) &len, sizeof(len));
    out.write(in.name.c_str(), len);
    out.write((char*) &(in.add_year), sizeof(in.add_year));
    return out;
}

const char * file = "students.dat";

int main()
{
    details x;

    ifstream ifile;
    ifile.open(file, ios_base::binary);
    if (ifile.is_open())
    {
        cout << "Contents of " << file << " is" << endl;
        while (ifile >> x)
        {
            cout << x.name << " - " << x.add_year << endl;
        }
        ifile.close();
    }

    cout << "Name:" << endl;
    getline(cin, x.name);
    cout << "Year added:" << endl;
    cin >> x.add_year;

    ofstream ofile(file, ios_base::app|ios_base::binary);
    if (!ofile.is_open())
    {
        cerr << "Can't open " << file << endl;
        exit(EXIT_FAILURE);
    }
    ofile << x;
    ofile.close();

    cin.get();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c ++使用std :: string读取和写入struct数组到二进制文件中 - c++ read and write struct array with std::string into binary file 写入和读取结构到文件 - Write and read struct to file 将文件内容读取到动态分配的char *数组中-我可以改为读取std :: string吗? - Reading contents of file into dynamically allocated char* array- can I read into std::string instead? 通过char写入字符串数组char - write to string array char by char 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员? - How to read/write string type member of a struct using binary file in/out in c++? 如何从char数组中读取“%”字符并将其使用fprintf_s写入文件? - how to read “%” character from char array and write it to a file using fprintf_s? 将包含字符串属性的结构读/写到二进制文件 - Read/Write struct containing string property to binary file 向文件写结构/从文件读结构 - Write/Read Struct To/From File 如何将recv返回的char数组写入文本文件,然后读回char数组 - How to write a char array returned by recv to a text file, and read back out into a char array 如何在结构中使用字符串而不是char数组来读取二进制数据 - How do I use a string in a struct instead of a char array for reading binary data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM