如何在c中使用fread()读取结构体的内容
我有以下代码,我试图读取文件的内容并显示它并写入另一个文件。我的问题是我在屏幕上看到的内容与文件的内容完全不同。我已经放置了文件的部分内容和显示的结果的部分
#include<iostream>
#include <stdint.h>
#include <stdio.h>
struct test
{
uint64_t start;
uint16_t length;
struct test *next;
};
void main()
{
char frd[32];
std::cout<<"\nEnter name of file to read?\n";
std::cin>>frd;
FILE *rd=fopen(frd,"r+b");
FILE *wrt=fopen("abc2.doc","w+");
struct test test_st;
while(fread (&test_st, sizeof(test_st), 1, rd))
{
fwrite (&test_st,sizeof(test_st),1,wrt);
printf("%llu,%u\n", test_st.start, test_st.length);
}
fclose(rd);
fclose(wrt);
}
源文件的部分内容:
0,43
43,95
138,159
297,279
576,153
729,64
显示的结果的前几行:
3474018176930688048,13879
3472896773804077344,14136
4049914982932231728,13362
3978707281317738034,12342
3474306356368193848,14132
3688511012684903220,14130
724298015681099573,13624
源文件和目标文件具有精确的副本
I have the following code where I am trying to read the content of a file and display it and also write to another file. My problem is the content i get on the screen is completely different from the content of the file. I have put parts of the contents of the file and parts of the result displayed
#include<iostream>
#include <stdint.h>
#include <stdio.h>
struct test
{
uint64_t start;
uint16_t length;
struct test *next;
};
void main()
{
char frd[32];
std::cout<<"\nEnter name of file to read?\n";
std::cin>>frd;
FILE *rd=fopen(frd,"r+b");
FILE *wrt=fopen("abc2.doc","w+");
struct test test_st;
while(fread (&test_st, sizeof(test_st), 1, rd))
{
fwrite (&test_st,sizeof(test_st),1,wrt);
printf("%llu,%u\n", test_st.start, test_st.length);
}
fclose(rd);
fclose(wrt);
}
Partial content of the source file:
0,43
43,95
138,159
297,279
576,153
729,64
First few lines of the result displayed:
3474018176930688048,13879
3472896773804077344,14136
4049914982932231728,13362
3978707281317738034,12342
3474306356368193848,14132
3688511012684903220,14130
724298015681099573,13624
The source and destination files have exact copies
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的文件不包含结构,它包含逗号分隔的值(数据的文本表示)。
结构以二进制形式存储,而不是文本形式。
当您读取时,您的程序会尝试将其读取的内容解释为二进制。撤消该操作:
要读取以文本形式存储的数据,您可以使用
fgets
读取每一行,然后解析它(例如:使用sscanf()
)。Your file does not contain a structure, it contains comma-separated values (a textual representation of data).
A structure is stored in binary form, not in text form.
When you read, your program tries to interpret what it reads as binary. Undoing that:
To read data stored in textual form, you can read each line with
fgets
, and then parse it (e.g: usingsscanf()
).这是一些工作代码。我离开了写作部分,因为我不知道你在寻找什么样的输出。只要浏览一下阅读逻辑,你就会知道如何修复写作部分。
我将调试 printf 留在那里,以便您可以了解代码如何工作以及如何解析 csv 文件以获得您正在寻找的结果。如上所述,该文件是文本(csv)文件而不是二进制文件。您尝试读取它的方式意味着读取二进制文件。所以这种方法不会有帮助。现在要读取二进制文件,您必须将它们存储为二进制文件。
Here is the some working code. I left the writing part as I dont know what kind of output you are looking for. Just go through the reading logic and you ll have an idea how to fix the writing part.
I left the debug printf there so that you can understand how the code is working and how the csv files is being parsed to get the results you are looking for. As mentioned above the file is a text(csv) file not a binary file. The way you are trying to read it is meant to read binary files. So that approach wont going to help. Now to read binary files you have to store them as binary.
写一个指向文件的指针是没有意义的。读回该指针的代码不会与写入该指针的代码具有相同的内存视图。所以不要这样做。
相反,在开始编写用于读取和写入二进制文件的代码之前,请先退后一步。二进制文件是字节流。因此,请定义文件中所需的字节流。然后编写代码来写入您定义的确切字节流。然后编写代码来读取具有您定义的确切字节流的文件。
然后,如果你遇到问题,你就会知道该责怪什么。检查文件中的字节并确保它们符合定义。如果没有,那是作者的错。如果是这样,那就是读者的错了。
It makes no sense to write a pointer to a file. The code that reads back that pointer won't have the same view of memory as the code that wrote it. So don't do that.
Instead, before you start writing code to read and write to and from binary files, step back for a second. A binary file is a stream of bytes. So define the stream of bytes you want in the file. Then write code to write the exact stream of bytes you defined. Then write code to read in a file that has the exact stream of bytes you defined.
Then, if you have a problem, you will know what to blame. Check the bytes in the file and make sure they comply with the definition. If not, it's the writer's fault. If so, it's the reader's fault.
您还应该打开文件来写入二进制文件:
You should open the file for writing binary as well: