c++ - 将 std::string 正确写入二进制文件

c++ - properly writing std::string to binary file(c++ - 将 std::string 正确写入二进制文件)

本文介绍了c++ - 将 std::string 正确写入二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 cin 获取 strings 并将其写入二进制文件.我读过写纯字符串是行不通的,所以我尝试将其转换为 char*.

I am trying to get strings from cin and than write it to binary file. I've read that writing pure string won't work, so I tried to convert it to char*.

下面的代码写得很好(...可能),但是只有前8个字符,所以文件中的输出不完整.

The following code writes it ok (...probably), but only the first 8 chars, so the output in file is incomplete.

std::string nick, ip, port;

std::cout << "IP: ";
std::cin >> ip;
std::cout << "port: ";
std::cin >> port;

ofstream file1("lastServers.bin", ios::out | ios::binary);
if (file1.good())
{
    const char* p_IP = ip.c_str();
    const char* p_PORT = port.c_str();
    int length = sizeof(&p_IP)+sizeof(&p_PORT);
    char* tmp1 = new char[length];

    int index = 0;
    memcpy((tmp1 + index), p_IP, sizeof(&p_IP));
    index = index + sizeof(&p_IP);
    memcpy((tmp1 + index), p_PORT, sizeof(&p_PORT));

    file1.write(tmp1, length);

    file1.close();
    delete[] tmp1;
}

else
{
    std::cout << "file error write" << endl;
}

<小时>

在此先感谢您的帮助 :)

推荐答案

你的代码可以写成

ofstream file1("lastServers.bin", ios::out | ios::binary);
if (file1.good()) {
    file1.write(ip.c_str(), ip.size());
    file1.write(port.c_str(), port.size());
    file1.close();
}
else {
    std::cout << "file error write" << endl;
}

string::c_str() 返回指向字符串中文本的 const 指针.string::size() 返回字符串中的字符数.

string::c_str() returns a const pointer to the text in the string. string::size() returns the number of characters in the string.

您不需要在写入文件之前连接数据,写入一个然后另一个具有相同的结果.

You don't need to concatenate the data before writing to the file, writing one then the other has the same result.

如果你想编写 C 类型代码而不是 C++,你可以使用 strlen(p_IP) 来获取 IP 字符串的长度,而不是使用 sizeof.

If you wanted to write C type code rather than C++, you can use strlen(p_IP) to get the length of the IP string rather than using sizeof.

sizeof 运算符为您提供类实例的大小,即对象的大小,但字符串对象的大小永远不会受到它所管理的字符串大小的影响.

The sizeof operator gives you the size of the class instance, i.e. the size of the object BUT the string object's size is never affected by the size of the string it manages.

在 C++ 中,管理某些东西的对象(想想管理字符的字符串、管理其内容的容器等)通常有一种方法来确定它们所管理的内容的大小.对于 std::string 和其他 STL 容器,该方法是 size().

In C++, objects that manage something (think strings managing characters, containers managing their contents, etc.) usually have a method to determine the size of what they're managing. For std::string and other STL containers that method is size().

请注意,以这种格式编写这些字符串意味着您无法分辨一个字符串的结束位置和另一个字符串的开始位置.要考虑的两个选项是使用您知道不会出现在任何字符串中的终止字符,或者在字符串本身的文本之前将字符串的长度写入文件.我不会在这里详细说明,因为原始问题中没有提出.

Note that writing these strings in this format means you can't tell where one string ends and another one starts. Two options to consider are using a terminating character that you know won't appear in any strings, or writing the length of the string to the file before the text of the string itself. I won't elaborate here as it was not asked in the original question.

这篇关于c++ - 将 std::string 正确写入二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:c++ - 将 std::string 正确写入二进制文件

基础教程推荐