Thread: Reading binary files

  1. #1
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587

    Reading binary files

    Can somebody please tell me how to read a text file as binary file?
    Thanks in advance.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fopen (fileName, "rb");
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Thanks for replying.
    How do I read?
    can I use fgetc() function or fread() function.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Typically you use fread.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    I tried to read a file like this:
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(void){
    FILE *fp;
    char c;
    fp=fopen("tw.txt","rb");
    while(=fgetc(fp)!=EOF){
    printf("%c\n",c);
    }
    return 0;
    }
    I got the output as face ASCII chars.
    Actually the input contained some text.
    How to overcome this?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Typically you use fread.
    Why? The different I/O functions solve different problems. Just because fread and fwrite have traditionally been associated with binary streams doesn't mean they're always the best choice. The correct answer to the OP's question is "what are you trying to do?".

    >char c;
    fgetc returns int. If char is unsigned for your implementation, your loop would run forever because EOF is a negative value.

    >How to overcome this?
    Overcome what? Can you be more specific about what the problem is?
    My best code is written with the delete key.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent:
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(void){
    	FILE *fp;
    	char c;
    	fp=fopen("tw.txt","rb");
    	while(=fgetc(fp)!=EOF){
    		printf("&#37;c\n",c);
    	}
    	return 0;
    }
    What were you expecting?

    Quote Originally Posted by Prelude View Post
    >Typically you use fread.
    Why? The different I/O functions solve different problems. Just because fread and fwrite have traditionally been associated with binary streams doesn't mean they're always the best choice. The correct answer to the OP's question is "what are you trying to do?".
    Of course, but I did mention typically, didn't I?
    Fread is good for most things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    I want the o/p to be in binary format.
    And I was experimenting by printing it as character.
    Please bear with my ignorance.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but I did mention typically, didn't I?
    You did, but I disagree that it's typical, which is why I asked you why.

    >I want the o/p to be in binary format.
    You're not going to get 0's and 1's as output, if that's what you were expecting. The only difference between binary mode and text mode is that text mode supports conversions conversions such as '\n' while binary mode gives you the raw characters without any filtering.
    My best code is written with the delete key.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Reading "binary" only means that certain characters won't be interpreted as "end of file characters" and that returns are translated to "\n". Nothing more.
    The type of your variable that you read into determines the output you get.

    Prelude: well, for binary, I have had the need of anything else than fread/fwrite (no, I don't like C++ I/O).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Actually I want the file to be printed as it is stored on the disk.
    i.e in 1's and 0's.Is there any way to do it?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not stored in 1's and 0's. It's stored in decimal form.
    But of course it's possible. By converting to binary. But that in itself is not the easiest thing.

    This function can be used for print binary information:
    Code:
    void PrintBits(uint32_t nNumber)
    {
    	char Binary[sizeof(nNumber) * 8 + 1] = {0};
    	char Binary2[sizeof(nNumber) * 8 + 1] = {0};
    	for (int i = sizeof(nNumber) * 8 - 1; i >= 0; i--)
    	{
    		if (nNumber & (0x1 << i))
    			Binary[i] = '1';
    		else
    			Binary[i] = '0';
    	}
    	for (int i = sizeof(nNumber) * 8 - 1, j = 0; i >= 0; i--, j++) Binary2[j] = Binary[i];
    	/*for (int i = 0; i < sizeof(nNumber) * 8; i++)
    	{
    		//if (i == 1) cout << " ";
    		//else if (i == 1 + 8) cout << " ";
    		cout << Binary2[i];
    	}*/
    	puts(Binary2);
    	puts("\n");
    }
    
    FILE* f = fopen("myfile", "rb");
    char c = fgetc(f);
    PrintBits(uint32_t(c));
    This may be a bit above your current level of experience, as it's not that simple.
    Last edited by Elysia; 03-06-2008 at 01:53 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Actually I want the file to be printed as it is stored on the disk.
    Actually, you want the output to be represented as binary values (a common question). You need to understand that 'A' is just a representation of a value, 0x41 and 01000001 are also just representations of the same value. You're never going to get the file printed as it's stored on the disk unless you want to draw the molecular structure of your hard drive.
    My best code is written with the delete key.

  14. #14
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Thank you.This cleared up a few doubts i had.
    I will try this and post if i had any other questions.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    It's not stored in 1's and 0's. It's stored in decimal form.
    on disk? I think you got it switched around.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file in binary question
    By Dan17 in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 12:28 AM
  2. Reading Binary files
    By earth_angel in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2005, 06:48 AM
  3. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM