Help with file access in XP, for .png files

Started by
6 comments, last by Dougie 20 years, 3 months ago
Hi there, I''m having a major problem with the ReadFile function in VC++ 6. When I run the code below, if I''m trying to read a .png file, it fills the buffer with a whole mess of crap. Code: char* filePath = new char[1024]; unsigned __int8 buf[36]; ... HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); ReadFile(hFile, &buf[0], 36, &Read, NULL); CloseHandle(hFile); Ok, filePath is the entire file''s path, which I get from windows common dialog box. I''ve tried everything, and found that a .txt file or a .txt file renamed as a .png works fine. But for some reason it doesn''t like proper .png''s. Previously, I used a char buffer, and somehow the values ended up negative. Anyway, I''m using Windows XP home edition. Can anyone suggest code fixes?
Advertisement
Why not make a structure that is the same format as the png file header and read into that?
.
It would make no difference. The data been read is not the same as in the file it''s been read from. I checked
I''ll guarantee that it is the same. The Win32 API does not discriminate based upon a file''s extension. You are simply misreading what has been read in.
.
You got negative values because char is signed. Use unsigned char instead.
I tried that, the values are still not correct
whoops... umm, I was reading the wrong section of bytes. I was reading 16 bytes off for what I wanted.
Consider using memory-mapped files. Basically that will give you a memory-pointer to read from and the OS will read from file as needed. Less pain for you and likely faster as you don''t need to copy any data.

A search for "memory-mapped" on http://codeproject.com/ will give you something to start with.

This topic is closed to new replies.

Advertisement