【问题标题】:How to open and read binary files from command line in a c program如何在 C 程序中从命令行打开和读取二进制文件
【发布时间】:2016-03-11 10:40:15
【问题描述】:

我正在尝试用 C 语言编写代码以使用 linux 从命令行读取二进制文件。文件读取规范如下:

*输入文件的名称将作为命令行参数传递给程序。

*程序将打开这个二进制文件并读取文件中的第一个整数。然后它将使用 malloc 函数动态创建一个此大小的浮点数组。

*然后程序将读取浮点值并将它们存储到这个新创建的数组中。

我唯一能成功做的就是打开文件。我之前尝试通过做来分配一块内存

file=double*malloc(30*sizeof(double));

但是当我尝试将文件放入 fread 函数的 *ptr 参数时,我不断收到错误并遇到了一些严重的问题

 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

这是我目前所拥有的:

#include <stdio.h>

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) 
    {

        printf( "usage: %s filename", argv[0] );
    }
    else 
    {

        FILE *file = fopen( argv[1], "r" );


        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int x;

            while  ( ( x = fgetc( file ) ) != EOF )
            {
                printf( "%c", x );
            }
            fclose( file );
        }
    }
}

【问题讨论】:

  • "尝试将文件放入 fread 函数的 *ptr 参数时遇到了一些严重问题"。你读过fread man page吗?它告诉您ptr 参数应该是什么?为什么你会认为file 进入ptr 参数而不是stream 参数?即使您没有阅读手册页,类型也会告诉您。请先仔细阅读手册页,因为它确实回答了您的具体问题。回来询问您何时完成尽职调查,但仍不清楚。
  • 刚刚注意到您对打开的文件流和数据缓冲区都使用了file。一个在实际代码中,一个在问题描述中。在您的实际代码中不可能是这种情况,因此请说明您的确切代码以及该代码的确切错误。

标签: c linux ubuntu command-line binary


【解决方案1】:

也许是这样的?

    int size;
    float *floatArray;
    FILE *file = fopen( argv[1], "r" );

    fread(&size, sizeof(int), 1, file);
    floatArray = (float*) malloc(sizeof(float) * size);
    fread(floatArray, sizeof(float), size, file);

    for(int i = 0; i < size; i++)
    {
        printf("%d: %f\n", i+1, floatArray[i]);
    }

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    #include <stdlib.h> /* for malloc() and exit() function */
    
    int main(int argc, char* argv[])
    {
        FILE *file;
        int i, n; /*  the counter and num of floating point numbers */
        float* val; /* pointer for storing the floating point values */
        if(argc<2)
        {
            printf( "usage: %s filename", argv[0] );
            exit(-1);
        }
        if((file = fopen( argv[1], "rb" )) ==NULL)
        {
            printf( "Could not open file.\n" );
            exit(-2);
        }
        if(fread(&n, sizeof(int), 1, file)!=1)
        {
            printf( "Could not read data from file.\n" );
            exit(-3);
        };
    
        if((val = malloc(n*sizeof(float)))==NULL)
        {
            printf( "Could not allocate memory for data.\n" );
            exit(-4);
        };
        printf("Number of data values: %d\n", n);
        if(fread(val, sizeof(float), n, file)!=n) /* read each floating point value one by one */
        {
            printf( "Could not read data from file.\n" );
            exit(-5);
        }
        for(i=0; i<n; ++i)
        {
            printf("%f \t", val[i]); /* now print it */
        }
        free(val); /* free the allocated memory */
        fclose(file); /* close the file */
        return 0;
    }
    

    注意:

    1. 假设int的大小与数据文件中的相同,而不是shortlong或其他任何东西。
    2. 此外,数据文件字节序被假定为与运行上述代码的机器相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多