插入字符串的额外字符(我插入了空字符'\ 0')

时间:2015-03-18 17:40:03

标签: c++ string fstream randomaccessfile

我一直在使用STL的fstream对象的read()成员函数遇到麻烦。我有一个ClientData类,有四个成员变量(int accountNumber,string firstName,string lastName,double balance)。

当我读取信息时,我会在每个字符串的末尾添加一些未输入的字符。如果我输入" AVeryLongWord"然后是" ShortOne"结果是" ShortOnegWord"或类似的东西。

我有一个名为clientData的ClientData对象并存储/检索信息,我使用以下语句:

写:

nombrArchivo.write(reinterpret_cast<const char*>(&clientData), sizeof(clientData));

要阅读我使用的数据:

archivo.seekg(0);
archivo.read(reinterpret_cast< char*>(&cliente), sizeof(ClientData));

设置我使用的字符串:

int length = fName.size();
length = length<15?length:14;
fName.copy(firstName, length);
fName[length]='\0';

整件事是here。它是Mingw(GCC 4.6)的CodeBlocks(13.12)项目。

0 个答案:

没有答案