如何使用fread()在c中读取结构的内容 [英] how to read contents of a structure using fread() in c

查看:241
本文介绍了如何使用fread()在c中读取结构的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,我试图读取一个文件的内容并显示它,还写入另一个文件。我的问题是我在屏幕上获得的内容是完全不同的文件的内容。我已经把文件的部分内容和显示的部分结果显示

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);
}

源文件的部分内容:

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

源文件和目标文件有完全相同的副本

The source and destination files have exact copies

推荐答案

这里是一些工作代码。我离开了写部分,因为我不知道你正在寻找什么样的输出。只要通过阅读逻辑,你会有一个想法如何修复写入部分。

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.

我离开了调试printf在那里,让你可以理解代码是如何工作的以及如何解析csv文件以获取您要查找的结果。如上所述,文件是文本(csv)文件而不是二进制文件。你试图读它的方式是读二进制文件。所以这种方法不会帮助。现在读二进制文件,你必须将它们存储为二进制。

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.

#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

struct test {
  uint64_t start;
  uint16_t length;
  struct test *next;   
};

int main(void)
{
  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;
  char readLine[100];

  while(fgets(readLine, 100, rd) != NULL) {
    // Removing the new line from the end
    // This is a quick hack as Windows have two characters
    // to represent new line. It is not needed to remove newline.
    // I did so that printf output look pleasing
    readLine[strlen(readLine) - 1] = '\0';
    printf("\nr=%s", readLine);

    // Splitting the string based on ','
    // and then converting it to number
    char *token = NULL;
    token = strtok(readLine, ",");
    test_st.start = atol(token);
    printf("\nt1=%s, test_st.start=%llu", token, test_st.start);

    token = strtok(NULL, ",");
    test_st.length = atoi(token);
    printf("\nt2=%s,test_st.length=%d", token, test_st.length);
    //fwrite (&test_st,sizeof(test_st),1,wrt);
    //printf("%llu,%u\n", test_st.start, test_st.length);
  }
  fclose(rd);
  fclose(wrt);
  return 0;
}

这篇关于如何使用fread()在c中读取结构的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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