[英]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
,而且还可以保存到使用文件read
和write
的功能没有得到一个运行时错误?
#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.