1

I am trying to read a binary file and its content.

/*aoObj.fb is the pointer of the file (e.x. FILE *fp)*/

char ch;
aoObj.fp = fopen(aoObj.f_name, "rb");

if (aoObj.fp == NULL)

      {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
      }

        /* /\*list all strings *\/ */
        printf("\n\nThe content of the file: \n");

        while ((ch = fgetc(aoObj.fp)) != EOF)
          printf("%c", ch);

        fclose(aoObj.fp);
        (void) opt_free(&aoObj);
        return 0;
}

But I am facing issues when I print the content of thi file because only the first character of the input isn't printed ok, as follows:

enter image description here

May I know why this is happening ?

EDIT: All the variables which are being read are declared as STRINGS

9
  • Maybe UTF-8 BOM? Commented Jun 7, 2020 at 15:08
  • 3
    Change char ch to int ch and I guess you could use getc() too. See if it solves. Commented Jun 7, 2020 at 15:09
  • 2
    fgetc() returns an int, not a char. Commented Jun 7, 2020 at 15:09
  • Nope, neither getc() nor the int char didn't solve the issue Commented Jun 7, 2020 at 15:12
  • the posted code does not compile! Please post a minimal reproducible example so we can reproduce the problem and help you debug it. Commented Jun 7, 2020 at 17:20

1 Answer 1

2

The OP states the file contents are 'binary' not 'text' Therefore, accessing the file should be via the I/O operators made for binary files,

Suggest:

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

Since the data read from the 'binary' file is NOT ascii characters, it is an 'error' to try to print those 'binary' characters with the 'output format conversion' specifier: %c.

Suggest:

printf( "%02x\n", ch );

Note: the %02x so a leading nibble of 0x0 will be printed rather than suppressed.

When the code is corrected to use: fread() rather than fgetc() the declaration of ch can/should be unsigned char ch; so no need to change that to int ch;

The following proposed code:

  1. cleanly compiles
  2. performs the desired functionality
  3. is missing a main() function and the passing of the parameter: f_name so does not link
  4. properly checks for an error when opening the input file
  5. uses the returned value from fread() to 'assume' EOF, however, it may be instructive (and for robust code) to check the value of errno just to assure there was no other error.
  6. documents why each header file is included

Note: the proposed code is not very efficient as it only reads a single byte at a time rather than a whole buffer full of bytes

Note: the proposed code will output one byte contents (in hex) on a single line. You might want to modify that to output several bytes contents (in hex) before moving to a new line.

and now, the proposed code:

#include <stdio.h>    // FILE, fopen(), perror(), printf(), fclose()
                      // fread()
#include <stdlib.h>   // exit(), EXIT_FAILURE

void myfunc( char *f_name )
{
    unsigned char ch;
    FILE *fp = fopen( f_name, "rb");
    if (fp == NULL)
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    /* /\*list all strings *\/ */
    printf("\n\nThe content of the file: \n");

    size_t bytesRead;
    while ( ( bytesRead = fread( &ch, 1, 1, fp ) ) == 1 )
    {
        printf("%02x\n", ch);
    }

    fclose(fp);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Note that printf("%02x\n", ch); may print FFFFFFFF when ch = -1; Suggest char ch; --> unsigned char ch;
I do mean -1. When ch is signed., it typically has values in the [-128 ... 127] range. If ch had the value of 0x81 or 129, is is likely an unsigned char and pose no trouble here.
@chux-ReinstateMonica, As usual, your correct. BTW: I modified the code to have char ch = (char)0x81 and it output ffffff81, so any negative value would be a problem. I'll fix the answer.

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.