Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;使用fstream从二进制文件读取字符串_C++_Fstream - Fatal编程技术网

C++ c++;使用fstream从二进制文件读取字符串

C++ c++;使用fstream从二进制文件读取字符串,c++,fstream,C++,Fstream,我试图从二进制文件中读取字符串,但似乎无法使其正常工作。对C++来说,我是个新手。有人能帮忙吗?谢谢 string Name = "Shaun"; unsigned short int StringLength = 0; int main() { StringLength = Name.size(); ofstream oFile("File.txt", ios::binary|ios::out); oFile.write((char*)&StringLeng

我试图从二进制文件中读取字符串,但似乎无法使其正常工作。对C++来说,我是个新手。有人能帮忙吗?谢谢

string Name = "Shaun";
unsigned short int StringLength = 0;

int main()
{
    StringLength = Name.size();

    ofstream oFile("File.txt", ios::binary|ios::out);
    oFile.write((char*)&StringLength, sizeof(unsigned short int));
    oFile.write(Name.c_str(), StringLength);
    oFile.close();

    StringLength = 0;
    Name = "NoName";

    ifstream iFile("File.txt", ios::binary|ios::in);
    if(!iFile.is_open())
        cout << "Failed" << endl;
    else
    {
        iFile.read((char *)&StringLength, sizeof(unsigned short int));
        iFile.read((char *)&Name, StringLength);
    }

    cout << StringLength << " " << Name << endl;

    system("Pause>NUL");
    return 0;
}
string Name=“Shaun”;
无符号短整型字符串长度=0;
int main()
{
StringLength=Name.size();
文件流(“File.txt”,ios::binary | ios::out);
oFile.write((char*)&StringLength,sizeof(unsigned short int));
write(Name.c_str(),StringLength);
oFile.close();
StringLength=0;
Name=“NoName”;
ifstream iFile(“File.txt”,ios::binary | ios::in);
如果(!iFile.is_open())

cout您需要创建一个char类型的缓冲区

char *buffer = new char[size];
然后使用缓冲区作为参数来读取函数

iFile.read(buffer, size);

这是一条有问题的路线

    iFile.read((char *)&Name, StringLength);
您正在将
std::string的
char*
部分直接读取到
Name
的内存中

您需要同时保存字符串和字符串的大小,以便在读取数据时知道需要多少内存来读取数据

而不是

oFile.write(Name.c_str(), StringLength);
您需要:

size_t len = Name.size();
oFile.write(&len, sizeof(size_t));
oFile.write(Name.c_str(), len);
iFile.read(&len, sizeof(size_t));
char* temp = new char[len+1];
iFile.read(temp, len);
temp[len] = '\0';
Name = temp;
delete [] temp;
在返回途中,您需要:

size_t len = Name.size();
oFile.write(&len, sizeof(size_t));
oFile.write(Name.c_str(), len);
iFile.read(&len, sizeof(size_t));
char* temp = new char[len+1];
iFile.read(temp, len);
temp[len] = '\0';
Name = temp;
delete [] temp;
而不是这个

iFile.read((char *)&Name, StringLength);
试试这个:

Name.resize(StringLength);
iFile.read((char *)&Name[0], StringLength);

原始行从一开始就覆盖字符串对象数据,例如,可能包含字符串长度和容量,而不是字符数据。此外,您没有适当地调整字符串大小以包含数据。

从概念上讲,您需要了解正在读取的数据流。并非所有内容都包含字符数据使用8位字节大小的se ASCII。我还注意到您没有将读取的位大小设置为{R,B,G,a}颜色集的大小。使用基本的二维重复结构化代码