【问题标题】:How do I read an array or variable that is stored in a binary file, and print it out in the program?如何读取存储在二进制文件中的数组或变量,并在程序中打印出来?
【发布时间】:2019-05-13 01:29:50
【问题描述】:

我无法从用另一个 c 程序编写的文件中获取变量/元素的值以存储到正在运行的 c 程序的变量/元素中。 在将变量“i”读入程序(这是一个已经确定并存储在文件“inventory.txt”中的整数)时,我不断收到分段错误。 “i”表示正在打印的数组的元素数量(大小)。这是损坏的代码):。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int l, k;
    int *pn, *arraysize, *q;
    float *p;
    FILE *fp = fopen( "inventory.txt", "r+b" ); //declarations and I open up the binary file "inventory.txt"

    if ( fp == NULL )
    {                           //check file
        printf( "nah" );
        exit( EXIT_FAILURE );
    }

    *arraysize = fread( &arraysize, sizeof( int ), 1, fp );     //set arraysize in this program equal to i in the binary file inventory.txt?????? Here is where I get segmentation fault

    printf( "marker1" );        //place marker, I am not getting to this point when running the program

    for ( l = 0; l < *arraysize; l++ )
    {
        q[l] = fread( &q[l], sizeof( int ), arraysize, fp );    //set array elements ("arraysize" of them) pn, q, and p to the array elements of the same name in binary file inventory.txt
        p[l] = fread( &p[l], sizeof( int ), arraysize, fp );
        pn[l] = fread( &pn[l], sizeof( int ), arraysize, fp );
    }

    printf( "Below are the items in your inventory.\nPart#\tQuantity\tPrice\n" );       //print out the values
    for ( l = 0; l < *arraysize; l++ )
    {
        printf( "%5d\t", pn[l] );
        printf( "%8d\t", q[l] );
        printf( "%9.2f\n", p[l] );
    }
    return 0;
}

【问题讨论】:

    标签: c file io binary


    【解决方案1】:

    下面是你的一些代码:

    int *arraysize;
    *arraysize=fread(&arraysize,sizeof(int),1,fp);
    

    您将arraysize 声明为int *(一个指针),但随后您尝试将一个int 读入其中。这是没有意义的。然后您尝试取消引用它以存储返回值,该值崩溃,因为它不是有效的指针。

    你可能想要

    int arraysize;
    if (fread(&arraysize, sizeof(int), 1, fp) != 1) {
        fprintf(stderr, "error reading file\n");
        exit(1); }
    

    将arraysize 声明为一个简单的int(不是指针)并读入它。

    您的其他 fread 调用也存在类似问题。

    【讨论】:

      【解决方案2】:

      您的指针有点错误,代码从未分配任何内存来读取值,fread() 返回读取的项目数,代码(出于某种原因)用这个结果覆盖刚刚读取的值。

      这是一个补丁版本,但您没有包含任何测试数据,因此未经测试。

      #include <stdio.h>
      #include <stdlib.h>
      
      int main(  )
      {
          int    l;
          int   *pn;
          int   *q;
          float *p;
          int    arraysize;
      
          FILE *fp = fopen( "inventory.txt", "r+b" ); //declarations and I open up the binary file "inventory.txt"
      
          if ( fp == NULL )
          {                           //check file
              printf( "nah" );
              exit( EXIT_FAILURE );
          }
      
          fread( &arraysize, sizeof( int ), 1, fp );     //set arraysize in this program equal to i in the binary file inventory.txt?????? Here is where I get segmentation fault
      
          printf( "marker1" );        //place marker, I am not getting to this point when running the program
      
      
          // Allocate some memory to hold the data (Yes! With cast on malloc())
          q  = (int *)malloc(arraysize * sizeof( int ));  // TODO - check malloc() OK
          pn = (int *)malloc(arraysize * sizeof( int ));  // TODO - check malloc() OK
          p  = (float *)malloc(arraysize * sizeof( float ));  // TODO - check malloc() OK
      
          // read each entire block in a single operation
          fread( q, sizeof( int ), arraysize, fp );    //set array elements ("arraysize" of them) pn, q, and p to the array elements of the same name in binary file inventory.txt
          fread( p, sizeof( float ), arraysize, fp );
          fread( pn, sizeof( int ), arraysize, fp );
      
      
          printf( "Below are the items in your inventory.\nPart#\tQuantity\tPrice\n" );       //print out the values
          for ( l = 0; l < arraysize; l++ )
          {
              printf( "%5d\t", pn[l] );
              printf( "%8d\t", q[l] );
              printf( "%9.2f\n", p[l] );
          }
          return 0;
      }
      

      【讨论】:

      猜你喜欢
      • 2021-01-10
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多