如何在c程序中从命令行打开和读取二进制文件

时间:2022-04-04 07:07:38

I'm trying to write a code in C to read binary files from the command line using linux. The specifications on reading the file are as follows:

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

*The name of the input file is to be passed into the program as a command line argument.

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

*The program will open this binary file and read the first integer in the file. It will then dynamically create an array of floats of this size using the malloc function.

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

*The program will then read the floating point values and store them into this newly crated array.

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

The only thing I've been able to do successfully is open the file.I previously tried to allocate a block of memory by doing

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

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

But I kept getting errors and ran into some serious problems when trying to put file in the *ptr parameter of the fread fucntion

但是当我试图将文件放入fread fucntion的* ptr参数时,我一直遇到错误并遇到了一些严重的问题

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

This is what I have so far:

这是我到目前为止:

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

2 个解决方案

#1


1  

Maybe something like this?

也许是这样的?

    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


1  

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

NB:

注意:

  1. It is assumed that the size of int is same as in the data file and is not short, long or anything else.
  2. 假设int的大小与数据文件中的大小相同,并且不短,长或其他任何东西。
  3. Also, the endianness of the data file is assumed to be same as that of the machine on which this above code is run.
  4. 此外,假定数据文件的字节顺序与运行上述代码的机器的字节顺序相同。

#1


1  

Maybe something like this?

也许是这样的?

    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


1  

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

NB:

注意:

  1. It is assumed that the size of int is same as in the data file and is not short, long or anything else.
  2. 假设int的大小与数据文件中的大小相同,并且不短,长或其他任何东西。
  3. Also, the endianness of the data file is assumed to be same as that of the machine on which this above code is run.
  4. 此外,假定数据文件的字节顺序与运行上述代码的机器的字节顺序相同。