C++和二进制文件 - 新手问题

发布于 2024-11-07 04:36:31 字数 1151 浏览 4 评论 0原文

我有以下代码,我正在尝试在二进制文件中写入一些数据。 问题是我没有任何二进制文件的经验,我无法理解我在做什么。

#include <iostream>
#include <fstream>
#include <string>

#define RPF 5

using namespace std;

int write_header(int h_len, ofstream& f)
{
    int h;
    for(h=0;h<h_len;h++)
    {
        int num = 0;
        f.write((char*)&num,sizeof(char));
    }
    return 0;
}
int new_file(const char* name)
{
    ofstream n_file(name,ofstream::binary);
    write_header(RPF,n_file);
    n_file.close();
    return 0;
}

int main(int argc, char **argv)
{
    ofstream file("file.dat",ofstream::binary);
    file.seekp(10);
    file.write("this is a message",3);
    new_file("file1.dat");
    cin.get();
    return 0;
}

1. 如您所见,我正在打开 file.dat 并在单词“thi”内写入。然后我打开该文件并看到它的 ASCII 值。为什么会出现这种情况?

  1. 然后我创建一个新文件 file1.dat 并尝试在其中写入数字 0 五次。 我应该用什么?

这个

f.write((char*)&num,sizeof(char));

或这个

f.write((char*)&num,sizeof(int));

以及为什么我不能按原样写入数字的值并且我必须将其转换为 char*? 这是因为 write() 像这样工作还是我只能将字符写入二进制文件?

谁能帮助我理解发生了什么事?

I have the following code and i am trying to write some data in a binary file.
The problem is that i don't have any experience with binary files and i cant understand what i am doing.

#include <iostream>
#include <fstream>
#include <string>

#define RPF 5

using namespace std;

int write_header(int h_len, ofstream& f)
{
    int h;
    for(h=0;h<h_len;h++)
    {
        int num = 0;
        f.write((char*)&num,sizeof(char));
    }
    return 0;
}
int new_file(const char* name)
{
    ofstream n_file(name,ofstream::binary);
    write_header(RPF,n_file);
    n_file.close();
    return 0;
}

int main(int argc, char **argv)
{
    ofstream file("file.dat",ofstream::binary);
    file.seekp(10);
    file.write("this is a message",3);
    new_file("file1.dat");
    cin.get();
    return 0;
}

1. As you can see i am opening file.dat and writing inside the word "thi". Then i open the file and i see the ASCII value of it. Why does this happen?

  1. Then i make a new file file1.dat and i try to write in it the number 0 five times.
    What i am supposed to use?

this

f.write((char*)&num,sizeof(char));

or this

f.write((char*)&num,sizeof(int));

and why i cant write the value of the number as is and i have to cast it as a char*?
Is this because write() works like this or i am able to write only chars to a binary file?

Can anyone help me understand what's happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洒一地阳光 2024-11-14 04:36:31

函数 write() 是指向数据缓冲区的指针以及要流式传输到文件的数据的字节长度。因此,当您说

file.write("this is a message",3);

告诉 write 函数在文件中写入 3 个字节时。那就是“蒂”。

f.write((char*)&num,sizeof(char));

告诉 write 函数将 sizeof(char) 字节放入文件中。即 1 个字节。您可能需要它

f.write((char*)&num,sizeof(int));

,因为 num 是一个 int 变量。

Function write() that a pointer to your data buffer and the length in bytes of the data to be streamed to the file. So when you say

file.write("this is a message",3);

you tell the write function to write 3 bytes in the file. And that is "thi".

This

f.write((char*)&num,sizeof(char));

tells the write function to put sizeof(char) bytes in the file. That is 1 byte. You probably want it

f.write((char*)&num,sizeof(int));

as num is a int variable.

漫雪独思 2024-11-14 04:36:31
  1. 您正在将 ASCII 字符串“thi”写入 file.dat。如果您在十六进制编辑器中打开该文件,您将看到“74 68 69”,这是这些字符的数字表示形式。但是,如果您在理解 ASCII 的编辑器中打开 file.dat,它很可能会将这些值转换回其 ASCII 表示形式,以便于查看。在 ios::binary 模式下打开 ofstream 意味着数据按原样输出到文件,流不会事先应用任何转换。

  2. 函数ofstream::write(const char *data,streamsize len)有两个参数。 data 是这样的,因此 write 对单个字节进行操作。这就是为什么您必须首先将 num 转换为 char* 的原因。第二个参数len表示从data开始将写入文件的字节数。我的建议是使用 write(static_cast(num), sizeof(num)),然后将 num 设置为足够大的类型来存储所需数据。如果声明 int num,则在 32 位平台上,将向文件写入 20 个零字节。如果您只需要 5 个零字节,则声明为 char num

  1. You are writing the ASCII string "thi" to file.dat. If you opened the file in a hex editor, you would see "74 68 69", which is the numeric representations of those characters. But if you open file.dat in an editor that understands ASCII, it will most likely translate those values back to their ASCII representation to make it easier to view. Opening the ofstream in ios::binary mode means that data is output to file as is, and no transformations may be applied by the stream before hand.

  2. The function ofstream::write(const char *data, streamsize len) has two parameters. data is like this so that write is operating on individual bytes. That is why you have to cast num to a char* first. The second parameter, len, indicates how many bytes, starting from data that will be written to the file. My advise would be to use write(static_cast<char*>(num), sizeof(num)), then set num to be a type big enough to store the data required. If you declare int num, then on a 32bit platform, 20 zero bytes would be written to the file. If you only want 5 zero bytes, then declare as char num.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文