1

I have 2 c files and 1 header file. I am trying to read any bmp file and print out its information. But I keep getting very large numbers for each of the field variables and I do not know why. Any help would be appreciated thanks.

BMPread.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BMPreadfns.h"

int main( int argc, char *argv[] )
{
    //declare file read pointer
    FILE *file;

    //declare fileout read pointer
    //FILE *fileout; //declare file printed file pointer

    // open file 1 of argument counter and return 0 upon error
    if (!(file = fopen(argv[1], "rb")))return 0;

    short BMPid_mark;
    long BMPfile_len;
    short BMPreserved;
    long OffBits;
    long biSize;
    long biWidth;
    long biHeight;
    short biPlanes;
    short biBitCount;
    long biCompression;
    long biSizeImage;
    long biXPelsPerMeter;
    long biYPelsPerMeter;
    long biClrUsed;
    long biClrImportant;
    get_headervalue(&BMPid_mark,2,file);
    get_headervalue(&BMPfile_len,4,file);
    get_headervalue(&BMPreserved,2,file);
    get_headervalue(&OffBits,4,file);
    get_headervalue(&biSize,4,file);
    get_headervalue(&biWidth,4,file);
    get_headervalue(&biHeight,4,file);
    get_headervalue(&biPlanes,2,file);
    get_headervalue(&biBitCount,2,file);
    get_headervalue(&biCompression,4,file);
    get_headervalue(&biSizeImage,4,file);
    get_headervalue(&biXPelsPerMeter,4,file);
    get_headervalue(&biYPelsPerMeter,4,file);
    get_headervalue(&biClrUsed,4,file);
    get_headervalue(&biClrImportant,4,file);


    printf("\nType:%hd\n", BMPid_mark);
    printf("Size:%ld\n", BMPfile_len);
    printf("OffBits:%ld\n",OffBits);
    printf("biSize:%ld\n",biSize);
    printf("Width:%ld\n", biWidth);
    printf("Height:%ld\n", biHeight);
    printf("biPlanes:%hd\n", biPlanes);
    printf("biBitCount:%hd\n", biBitCount);
    printf("biCompression:%ld\n", biCompression);
    printf("biSizeImage:%ld\n", biSizeImage);
    printf("biXPelsPerMeter:%ld\n", biXPelsPerMeter);
    printf("biYPelsPerMeter:%ld\n", biYPelsPerMeter);
    printf("biClrUsed:%ld\n", biClrUsed);
    printf("biClrImportant:%ld\n\n", biClrImportant);


    fclose(file);

    return 0;
}

BMPreadfns.c

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

int get_headervalue(int *i,int bitSize, FILE *fp)
{
    fread(i,bitSize,1,fp);
}

BMPreadfns.h

#ifndef BMPREAD_BMPREADFNS_H
#define BMPREAD_BMPREADFNS_H
int get_headervalue(int *i,int bitSize, FILE *fp);
#endif //BMPREAD_BMPREADFNS_H
Master Maq
  • 11
  • 4
  • 1
    `fread(&i,bitSize,1,fp);` That writes to a **local** variable `i`. You need to declare the function to accept `int *` and pass in `&my_int_var`. – kaylum Feb 15 '17 at 02:52
  • I tried that but I get errors for different field types being passed – Master Maq Feb 15 '17 at 03:06
  • 1
    Your types are not all `int`. So you need different functions to do the read for different lengths or do some casting or have the function accept the type length, etc. Anyway, that is a different problem to your original issue. But the original issue is clear (I hope) - you are not modifying the caller's variable but a local function variable. – kaylum Feb 15 '17 at 03:10
  • `fread(&i,bitSize,1,fp);` No, not like that. `fread(i,bitSize,1,fp);` because `i` is already a pointer. – kaylum Feb 15 '17 at 03:14

0 Answers0