Click here to Skip to main content
15,795,331 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hy friends
I just want how work file in c programming and want idea how I can read file (maybe was image input or txt file )
-when I did when (c!=EOF) and c =fgetc(file)
c programming read the byte of file like a char it means a ASCII
but I want like read my file only binary 0 or 1 ?
how I can will do?
-convert all input file to binary (all byte to binary) and save they to another file binary
what yoou propose?
thnk you

What I have tried:

I want idea just for discussion
Updated 23-Jun-16 22:40pm
Sergey Alexandrovich Kryukov 23-Jun-16 22:18pm    
The question would really make sense if you explained 1) the purpose, 2) the problem.
Just reading a file hardly makes any sense. The whole idea of "convert" is wrong. There is no such thing as "not binary" file; all files are "binary". You just read raw data, that's it; there is nothing to "convert". But reading — to what data structure? It depends on purpose and some other factors.
yagami_md 23-Jun-16 23:54pm    
the problem is :
in c when you read a byte you indeed read the ascii of the byte it means 0 in ascii read like 48 in nteger and 1 ascii read 49 in integer (decimal)
my purpose is when I did fgetc (file) I read the ascii
I want no read like this , I want read 0 as (b7b6b5b4b3b2b1b0) because we have 255 ASCII ca read in file using c
ifyou not understand I can explain more friend
Sergey Alexandrovich Kryukov 24-Jun-16 0:43am    
What you say is not true. Not even close.

I don't even know how to explain it. The byte has nothing to do with ASCII. ASCII is encoding for text (and it's only for code points below 128). When you read a file, you read bytes, not text. Sorry, you are lost. This is you who don't understand. There are no any problem reading those bytes. Generally, to understand writing or reading file, it would be the best to forget that char or string even exist. Later on, you may learn how to combine text data with the rest. The idea is: you define some structure, get it's binary size and them write it all as a buffer; and symmetrically read, when you need to read...


There's no such thing as reading a bit, or what you're calling "binary". The lowest you can get is down to reading a single byte. From there, you have to parse the byte to get the bit values you want using bit masks and the logical and/or shift operators.
Share this answer
yagami_md 23-Jun-16 23:57pm    
you mean if I have a input file as a txt ={aabbcde)
I must convert all in another file conatains the binary of all (8char * 8 (ascii coding by 8 bits max 255in decimal
Dave Kreskowiak 24-Jun-16 0:12am    
That made no sense at all.

ALL files can be read as bytes. ASCII has nothing to do with it. That's an encoding, how the bytes are interpreted to form text.

Reading a byte and then getting the values of each bit of the byte has nothing to do with ASCII.

You have not explain what you mean by "convert the input file to binary" and "save them to another file binary". That makes no sense at all.

Are you asking how to read the bytes of a file, get the values of each bit, a 0 or 1, and then write the 0 and 1 values at text to another file?? Essentially making a file you can open in Notepad that will show nothing but 1's and 0's??
yagami_md 24-Jun-16 0:24am    
P5# Created by Imlib
512 512
¢¢¢¡¢£¡¦¢¢ ›£ ›œ¡¡šœš™š˜œššœšž› ž§ ¦¦¥¦¬«¯­ª¬¬§¯¨§¢¡“”™Œ‚wvjaa^\Xag`heiiiihnnlkihmmmljjlnmkmmnnlhjjlmovoqvywpzyz}zy|{‚{z„ƒ„‡€ƒƒ‡‰„†ƒƒ€‚†~~…‚ƒ€ƒ€‡…ˆ†€†‚‡……†‡„‡„ˆ†„……ˆ…††„…ƒ„…‡‰†…ƒ…ƒ‡ˆ„‡ˆ‰…Šˆ‡…‰‡ˆ„…Œ„ƒ‹†ƒ„ƒ~ƒƒ„ƒ„ƒ€‡……‡ƒ„……ƒ…‚ƒƒ…ˆ†„‡„‡†Šƒƒƒ……ŠŠŽ†„„€€…†„†…ƒ€ƒ„~‚‡†~‚ƒ†~„ƒ€ƒƒ}€‚~€„~y||ywzywuroqsifghptvz„‡‹Œ‘’—™˜™ŸŸŸ£ž¢¡››””™•š——›—›˜œššŸŸœžš™—™—š˜š™˜™˜——žšš˜˜™šžžššœ   œœ˜››ž±ÀÆÎÐÓÓÔ×ÚÚÚÚÜØÓÌÀ¬˜zkjeghoiqtuwuwvy{ywux}yv{z}€yw{vy|{xzyy{z|v|~|~~{{|}vv€z~~~‚|~~z‚|~w{rytwvsx|‰¢¨©«ª›€¢¢¢¡¢£¡¦¢¢ ›£ ›œ¡¡šœš™š˜œššœšž› ž§ ¦¦¥¦¬«¯­ª¬¬§¯¨§¢¡“”™Œ‚wvjaa^\Xag`heiiiihnnlkihmmmljjlnmkmmnnlhjjlmovoqvywpzyz}zy|{‚{z„ƒ„‡€ƒƒ‡‰„†ƒƒ€‚†~~…‚ƒ€ƒ€‡…ˆ†€†‚‡……†‡„‡„ˆ†„……ˆ…††„…ƒ„…‡‰†…ƒ…ƒ‡ˆ„‡ˆ‰…Šˆ‡…‰‡ˆ„…Œ„ƒ‹†ƒ„ƒ~ƒƒ„ƒ„ƒ€‡……‡ƒ„……ƒ…‚ƒƒ…ˆ†„‡„‡†Šƒƒƒ……ŠŠŽ†„„€€…†„†…ƒ€ƒ„~‚‡†~‚ƒ†~„ƒ€ƒƒ}€‚~€„~y||ywzywuroqsifghptvz„‡‹Œ‘’—™˜™ŸŸŸ£ž¢¡››””™•š——›—›˜œššŸŸœžš™—™—š˜š™˜™˜——žšš˜˜™šžžššœ   œœ˜››ž±ÀÆÎÐÓÓÔ×ÚÚÚÚÜØÓÌÀ¬˜zkjeghoiqtuwuwvy{ywux}yv{z}€yw

look " its when I open a file (like image ) by notpad !!!!! I see only AScii
Dave Kreskowiak 24-Jun-16 8:29am    
Aaaaaannnnnddddd that means absolutely nothing to me. AGAINS, what do you want to do with this file?
File content is always binary using 8-bit (byte) as smallest unit. What kind of data is represented depends on the file type and must be handled accordingly by the application when reading and writing the file.

The fgetc - C++ Reference[^] function returns a single byte read from the file as an int. The reason for returning an int instead of a byte type like char or unsigned char is that an additional return value for errors and end of file is required (EOF).

So you don't need any conversion. But you may need to interpret the data to prepare it for usage or just printing it out.

If you have no information about the file type, you can print the binary content:
#include <stdio.h>

int main(int argc, char* argv[])
    int c;
    FILE *f;
    if (argc > 1)
        f = fopen(argv[1], "rb");
        if (f)
                c = fgetc(f);
                if (EOF != c)
                    printf("%02X ", c);
            } while (EOF != c);
    return 0;

If you want to print each byte as bit sequence you have to write a corresponding function (call this instead of the printf function in the above example):
/* Print byte as bit sequence with MSB first */
void print_byte_as_bits(int byte_value)
    int i;
    if (byte_value >= 0 && byte_value < 256)
        for (i = 0; i < 8; i++)
            printf("%c ", (byte_value & 0x80) ? '1' : '0');
            byte_value <<= 1;
        printf("[Not a byte: %X]", byte_value);
Share this answer
yagami_md 24-Jun-16 11:44am    
thnk you very much friend

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900