c ++ - 正确地写std :: string到二进制文件 [英] c++ - properly writing std::string to binary file

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

问题描述

我想从 cin 取得 字符串 ,然后将其写入二进制文件。
我读到,写纯字符串将无法工作,所以我试图将其转换为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*.

以下代码将其写为ok。 。

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()返回字符串中的文本的常量指针。
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到二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆