Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey everyone
i have a image , i want when i start to read the image by the file in c programming , read by block or a limit number of byte defined before , it means read a limit byte for example 32 byte , after i want to calculate the probability for every byte in this block

What I have tried:

FILE *fp=fopen("image.pgm","rb")
while (c=fgetc(fp)!=EOF)
//read the 32 first byte
//calcule the probability of every byte
//return to position 32 in the file
Updated 20-Oct-16 3:31am

Share this answer
yagami_md 27-Aug-16 5:30am    
thank you , every one have a level question it means learn !
Patrice T 27-Aug-16 5:40am    
the 2 last links are the reference book by the authors of C Language.
Take time to read it, it is worth the cost.
Do not use fgetc to read files, especially binary data. Use fread as described at[^].
Share this answer
yagami_md 20-Oct-16 7:49am    
hi friend stopped my work since 2 month ..i want to end that ..please can you give me how read image by block? for example in FILE stream i would like read 64 byte by byte !!! thank you
Richard MacCutchan 20-Oct-16 8:00am    
Follow the link I gave you, it shows exactly how to do it.
yagami_md 20-Oct-16 8:03am    
please friend im a bgenner in c language i want help
that what i want do
FILE *stream ;

FILE *stream1;

stream= fopen(pFile, "rb");

//when stream still bytes in stream



numread = fread( stream1, sizeof( char ), 64, stream );

//treat the stream1

Richard MacCutchan 20-Oct-16 8:10am    
I have given you help - go and read the information on that link. There is no point in me repeating what is already provided.
yagami_md 20-Oct-16 8:24am    
i copy all the whole file to buffer ... how i can read from the buffer?
This is the very basic code to read the file
unsigned char buffer[64];	// buffer may be any size
FILE *fp = fopen("image.pgm", "rb");
int numread;

while (!feof(fp))  // repeat until reached end of file
    int index;	// index into the buffer
    numread = fread(buffer, sizeof(unsigned char), 64, fp);
    if (numread == 0)
        break;  // no data left to read
    for (index = 0; index < numread; ++index)
        // process each byte of the buffer
        int b = buffer[index];	// b contains the value of the current byte
        printf("byte %d: %d\n", index, b);
Share this answer

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