Thread: how can i manipulate a file in binary mode?

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    18

    how can i manipulate a file in binary mode?

    hi take a look at this source code (it doesn't have bugs):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
        FILE *fp;
        int c;
    
        if ( (fp = fopen("TEXT.TXT", "rb")) == NULL)
        {
            fprintf(stderr, "Error opening file.");
            exit(1);
        }
        while ((c = fgetc(fp)) != EOF)
        {
            printf("%c ", c);
        }
        fclose(fp);
        return 0;
    }
    (in TEXT.TXT there's one line of the alphabet's characters(abcd...))

    it runs fine but fgetc gives me the actual letters in the text file.
    what i wanted it to print was the binary values byte by byte (like 01011001)
    how can i achieve that?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Opening a file in "binary mode" doesn't make things "binary", it just ensures that the raw bytes are read as-is, whereas "text mode" will do implementation-defined conversions, e.g., translating 0xd 0xa combinations into a single 0xa on windows. On *nix OSes, there's no difference between text and binary modes.

    You're seeing characters because you're printing with the %c format. If you used the %d format you'd see the decimal value. With %x you would see hex. There is no standard format for binary. If you really want to see the bits, you can write your own function to print them.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void binary(char byte) {
        unsigned char mask = 0x80;
        for ( ; mask; mask >>= 1)
            putchar(byte & mask ? '1' : '0');
    }
    
    int main( void )
    {
        FILE *fp;
        int c;
     
        if ( (fp = fopen("TEXT.TXT", "rb")) == NULL)
        {
            fprintf(stderr, "Error opening file.");
            exit(1);
        }
    
        while ((c = fgetc(fp)) != EOF)
        {
            binary(c);
            putchar(' ');
        }
    
        putchar('\n');
        fclose(fp);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    18
    Quote Originally Posted by algorism View Post
    Opening a file in "binary mode" doesn't make things "binary", it just ensures that the raw bytes are read as-is, whereas "text mode" will do implementation-defined conversions, e.g., translating 0xd 0xa combinations into a single 0xa on windows. On *nix OSes, there's no difference between text and binary modes.

    You're seeing characters because you're printing with the %c format. If you used the %d format you'd see the decimal value. With %x you would see hex. There is no standard format for binary. If you really want to see the bits, you can write your own function to print them.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void binary(char byte) {
        unsigned char mask = 0x80;
        for ( ; mask; mask >>= 1)
            putchar(byte & mask ? '1' : '0');
    }
    
    int main( void )
    {
        FILE *fp;
        int c;
     
        if ( (fp = fopen("TEXT.TXT", "rb")) == NULL)
        {
            fprintf(stderr, "Error opening file.");
            exit(1);
        }
    
        while ((c = fgetc(fp)) != EOF)
        {
            binary(c);
            putchar(' ');
        }
    
        putchar('\n');
        fclose(fp);
        return 0;
    }
    I've read the book teach yourself c in 21 days from sams publishing and it didn't cover what you just did in the binary function. can someone explain to me what he did? also i need a book recommendation for more advanced c coding so i can understand these source codes better obviously the book i read doesn't just cut it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can i seek in a file in binary mode?
    By Mohsen Abasi in forum C Programming
    Replies: 5
    Last Post: 04-08-2017, 08:04 AM
  2. Replies: 15
    Last Post: 12-17-2008, 12:11 AM
  3. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  4. Replies: 3
    Last Post: 09-22-2005, 01:35 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 06:45 PM

Tags for this Thread