0

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 );
        }
    }
}
2
  • "ran into some serious problems when trying to put file in the *ptr parameter of the fread fucntion". Did you read the fread man page? What does it tell you the ptr parameter is supposed to be? Why would you think the file goes into the ptr parameter rather than the stream parameter? Even if you didn't read the man page the type tells you that. Please read the man page carefully first as it does answer your specific question. Come back to ask when you have done that due diligence and am still unclear. Commented Dec 6, 2015 at 21:54
  • Just noticed that you are using file for both the open file stream and the data buffer. One in the actual code and one in the question description. That can't be the case in your actual code so please clarify your exact code and what the exact error is for that code. Commented Dec 6, 2015 at 21:57

2 Answers 2

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]);
    }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.