Read png file binary

Hello I have a question how can i read a png file binary?

FILE *f = fopen("image.png","r");
How to use fread now for example just to printf the numbers from the file?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream in ("image.png");
    char c;
    while(in >> c)
    {
        std::cout << c << std::flush;
    }
}
Oh I forgot to say I am working with standard C not C# or C++
It needs to be opened in binary mode..

Also, you need to understand the structure of the file otherwise everything will look like numbers to the program..

The answer would depend on what exactly you are trying to do.. for some specific tasks libpng can help.
Last edited on
I would like to open png file and read its magic numbers to determine if it is a png file. I made a mistake it should be "rb". Now I just don't understand the syntax of a fread.
If reading magic number of png is the aim, you can open the png in a hex editor software

http://en.wikipedia.org/wiki/File:PNG-Gradient_hex.png
well yeah I can but I need to make a program :)
Use something like this, altough it is not 100% reliable if is a valid PNG or not:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>
int main ()
{
    FILE* fp = fopen ("image.png", "rb");
    if (fp == NULL)
    {
        perror ("Unable to open the image file\n");
        return EXIT_FAILURE;
    }
    char buf[8];
    if (fread (buf, 1, sizeof (buf), fp) != sizeof(buf))
    {
        perror ("Unable to read file contents (file may be empty)\n");
        fclose (fp);
        return EXIT_FAILURE;
    }
    if (buf[0] == 0x89 &&
            buf[1] == 0x50 &&
            buf[2] == 0x4E &&
            buf[3] == 0x47 &&
            buf[4] == 0x0D &&
            buf[5] == 0x0A &&
            buf[6] == 0x1A &&
            buf[7] == 0x0A)
    {
        printf ("%s\n", "PNG Image signature was found");
    }
    else
    {
        printf ("%s\n", "PNG Image signature not found");
    }
    fclose (fp);
    return 0;
}
1) Why does this only go through loop once? The thing I wanna do is read text from table besede which is name of the file and open that file. When the first loop is over i want to open next file.

1
2
3
4
5
6
7
8
9
10
11
int i = 0;
    while(i < 6){
        unsigned char n[23];
        FILE *f = fopen(besede[i],"rb");
        fread(n, sizeof(int),23,f);
        fclose(f);
        for(int j = 0; j <= 23; j++){
            printf("%d \n",n[j]);
        }
        i++;
    }


If I change besede[i] to besede[0] ... it works fine.

2) how do I compare unsigned char table with integer.
Last edited on
Anyone?
That code could give you a segmentation fault on fread line, you request 23 * sizeof(int) bytes when you only have a 23 bytes buffer.
so how can i change it to work ok + the while loop
I guess that was the problem. I changed the line to fread(n, 1, 23, f); and now the loop works. I still don't know why?
Topic archived. No new replies allowed.