0

I am trying to print the entire contents of a file in C that was stored in a previous function. The file size was read and dynamic memory allocation was used accordingly to create space relative to the file size. A pointer (fp) was then used to point to the newly allocated space. Then, the contents of the file were read into the new space. (I think this is where my error is).

// gloabl variables
unsigned char *fp = 0;          //pointer to navigate through the opened file.
unsigned char *fileStart = 0;   // pointer to save the start address of the file, in case you need to go back to start of file
unsigned char fileSize = 0;     // stores the size of file

/*   Use dynamic memory allocation to store the entire contents of the file and let that memory be pointed by 'fp'.
Save the start of file in 'fileStart' so that you use it to go to start of file in other functions.
After opening the file, read the file size and use it for dynamic memory allocation to read entire file.  */
void loadFile(const char *filename)
{
    FILE *p = fopen(filename, "rb");
    if (p == NULL) {
        printf("File not created, errno = %d\n", errno);
        return 1;
    }
    fseek(p, 0, SEEK_END); // seek to end of file
    fileSize = ftell(p); // get current file pointer
    fseek(p, 0, SEEK_SET); // seek back to beginning of file
    printf("File loaded. File size = %#x bytes\n", fileSize);
    fp = malloc(fileSize + 1);
    fread(fp, fileSize, 1, p);
    fileStart = &fp;
    fclose(p);
}

/*Display file in hex.
Display neatly the content of the file as seen in hex editor.
Even after closing the file with fclose(), we have the contents of the file in memory pointed by 'fp' (or 'fileStart' in loadFile()).
So you don't have to open and read the file again.*/
void displayBmpFile()
{
    printf("Hex view of loaded bmp file: \n");
    while(!feof(fileStart))
        printf("%d\t", fgetc(fileStart));
}

I recognize the error is in my first function. First, I am not sure if the contents of the file were stored properly. If the contents were stored properly, is fp correctly pointing to the file? Lastly, if everything previously is working correctly, does fileStart correctly point to the beginning of the file? (The file is a small hex file of four square colors).

bluesky11
  • 15
  • 1
  • 9
  • Shouldn't you cast the value returned by `malloc`, as in `fp = (char*)malloc(fileSize + 1);` – Abra Mar 16 '19 at 04:14
  • `fileStart = &fp` -> `fileStart = fp`, `fp` is already a pointer. `fp = malloc(fileSize + 1);` -- there is no `+ 1`, `fread` does not read a nul-terminated string. You will want to look at [**Why is while ( !feof (file) ) always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) in addition to the prototype for `fgetc`, e.g. `int fgetc(FILE *stream);`, your `fileStart` is not type `FILE *`. Your compiler should be *screaming* warnings at you, if not, enable warnings and try again. – David C. Rankin Mar 16 '19 at 04:15
  • @Abra See https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Marco Mar 16 '19 at 04:15
  • @DavidC.Rankin I fixed the first two errors you suggested. I cannot change the fileStart to a FILE * type, I am required to leave it as a pointer to an unsigned char. Any suggestions? – bluesky11 Mar 16 '19 at 04:27
  • Yes, (1) change `unsigned char fileSize = 0;` to `size_t fileSize = 0;` (your bitmap will surely be larger that 255 bytes.) Then (2) in `void displayBmpFile()` do `size_t n = fileSize; unsigned char *p = fileStart;` and `while (n--) printf("%hhu\t", *p++);` (or pick whatever format you like for output -- I would recommend the exact width specifier macros in `inttypes.h`, e.g. `PRIu8`) If you post the compilable version, I'm happy to help you get it running. – David C. Rankin Mar 16 '19 at 04:33
  • It works! If you want, you can post your comment for an answer and I will select it for the answer. Otherwise I can put it up. A couple things, I've never used `size_t` before, is this a special data type? For the format I am required to use `printf("%#x", *p++); ` If that is what you meant. EDIT: accidental syntax error when I implemented size_t – bluesky11 Mar 16 '19 at 04:44
  • You should also check return codes like e.g. on fseek() or fread(). BTW - malloc() returns void* only on ISO C because original C didn't know void. ;-) – reichhart Mar 16 '19 at 09:20
  • Thanks. For some reason my compiler wasn't picking up on those errors yesterday. Today, the compiler is giving me warning messages about the errors you mentioned. – bluesky11 Mar 17 '19 at 05:03

1 Answers1

0

change unsigned char fileSize = 0; to size_t fileSize = 0; (your bitmap will surely be larger that 255 bytes.) Then (2) in void displayBmpFile() do size_t n = fileSize; unsigned char *p = fileStart;and while (n--) printf("%hhu\t", *p++);

bluesky11
  • 15
  • 1
  • 9