简体   繁体   中英

How to open and read binary files from command line in a c program

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:

*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.

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

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

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

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]);
    }
#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. Also, the endianness of the data file is assumed to be same as that of the machine on which this above code is run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM