Advertisement

weird problem w/ file I/O

Started by May 22, 2001 03:56 PM
2 comments, last by arsenius 23 years, 8 months ago
ok, i''m too lazy to write a bitmap loader, so i decided to use the .raw file format if you don''t know what that is, its the simplest file format ever the file holds a user defined header of however many bytes you want, in my case 0. Then comes the 24-bit RGB data for each pixel. The file does not hold any other information so i should be able to read in this data 3 bytes at a time using the standard c++ ifstream type deal, and I can, as long as the image is smaller than 7 x 7 pixels, but if the image is larger, i can''t heres my code that should work, but doesn''t

#include 
#include 

int main()
{
	ofstream fout;
	ifstream fin;
	char r, g, b;

	fout.open("mesh.raw",ios::out,ios::binary);
	fin.open("circles.raw",ios::in, ios::binary|ios::nocreate);
	fin.unsetf(ios::skipws);
	fout.unsetf(ios::skipws);
	while(!fin.eof())
	{
		fin >> r >> g >> b;
		fout << r << g << b;
	}
	return 0;
}
 
really simple, it should just make a duplicate copy of the original file, but it doesn''t, it messes up all the data so that it has the right number of bytes, but the wrong values. The new file (mesh.raw) looks *similar* but not the same to the original. any ideas? Oh, and the file has to be a multiple of 3 bytes large, because it is in R G B format, so inputting 3 bytes at a time is technically ok. -arsenius ''after three days without programming, life becomes meaningless'' -The Tao of Programming
Isn''t the first byte/DWORD how big the header is then?


...
HBITMAP hBitmap = (HBITMAP) LoadImage(NULL, "mesh.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);

It''s really not that hard :p

http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/winui/resource_9fhi.htm


Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
I can see a couple of problems with the code snippet you posted:

1. the >> and << operators don''t work with binary file streams. Which makes sense, because they are formatting stream operators, and in a binary file formatting doesn''t make a lot of sense. You''re better off using the member functions read(char* buf, int size), which reads size bytes from the stream into buf, and write(const char* buf, int size), which writes sizebytes from buf to the stream.

2. you don''t need to unset the skipws flags if you use read and write.

3. while(!fin.eof()) won''t work, because fin.eof() will only tell you if eof is reached after you''ve read something. It won''t predict eof! Better is to use while (fin), which loop until fin is in an error state (which could be because of eof).

HTH
I tried using .read() and .write() also, it gave me the exact same results as I was getting with >> and <<

I don''t think the << and >> operators have to format a file however, if you use them as I was, they should just input/output a stream of bytes.

I finally gave up and just used C''s FILE fopen() getc() etc...
That was the only way I could get the damn thing to work

The code below would work with some .raw files I tried, but not with others. For example, when I made an image with MS paint, and saved it as .bmp and converted it to .raw w/ Paint Shop Pro, it would work, but if I just created a file in PSP, it didn''t work, very frustrating.



-arsenius
''after three days without programming, life becomes meaningless'' -The Tao of Programming

This topic is closed to new replies.

Advertisement