Advertisement

Iostream or stdio ??

Started by February 11, 2002 08:45 AM
8 comments, last by CEdCopy 23 years ago
Which sort of i/o is best? IOSTREAM or STDIO? oh and could someone help me with my BMP loader, im trying to use ifstream to open it but i keep getting errors... plus C++ Builder is crap compared to VC++ Heres my lil code snippet: BITMAPFILEHEADER bmpheaderfile ; BITMAPINFOHEADER bmpheaderinfo ; long imageSize; int colorMode; long imageIdx; unsigned char colorSwap; unsigned char *bmpData; ifstream fin(filename, ios::in | ios::binary); if(!fin) return 0; fin.read((char*) &bmpheaderfile, sizeof bmpheaderfile); fin.read((char*) &bmpheaderinfo, sizeof bmpheaderinfo); //colorMode = bmpheaderinfo.biBitCount/8; imageSize = bmpheaderinfo.biWidth * bmpheaderinfo.biHeight * 3; if(bmpheaderinfo.biBitCount != 24) { fin.close(); return 0; } bmpData = new unsigned char [imageSize]; fin.seekg( bmpheaderfile.bfOffBits,ios::beg); fin.read((char*) &bmpData, sizeof bmpData); //ead(bmpfile->imageData, sizeof(unsigned char), imageSize, fileptr); for(imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode); { colorSwap = bmpData[imageIdx]; bmpData[imageIdx]= bmpData[imageIdx+2]; bmpData[imageIdx+2] = colorSwap; } fin.close(); Wot Sig?
Wot Sig?
1: there is no better I/O. It is which ever style suits you best.
2: If you included the errors it would be more helpful for me helping you since staring at your code will get me no where(Thats just me though.)
Advertisement
sorry.. the error is todo with the memory so i fink its this bit of code:

bmpData = new unsigned char [imageSize];

fin.seekg( bmpheaderfile.bfOffBits,ios::beg);

fin.read((char*) &bmpData, sizeof bmpData);

and when i delete bmpData it crashes. Maybe im not using the fstream properly?
Wot Sig?
hi,

i think you are not allowed to "new" a char.
to allocate chars you need malloc.
so try something like:

"bmpData= malloc(sizeof(unsigned char)* imageSize);"
(i´m not sure if there have to be an pointer!)

here a note from the msdn:
"The new operator does not allocate reference types because they are not objects."

and your
"fin.read((char*) &bmpData, sizeof bmpData);"
looks strange, too.
sizeof returns the size of the datatype, so i think you need
"..., sizeof(bmpData)* imageSize);"

hope this helps...

greetings
quote:
Original post by Rafflesia
i think you are not allowed to "new" a char.

Yes, you are.

Just ignore what Rafflesia said. Here''s some food for thought, oh, and i''m assuming (by your code) that you''re only reading 24-bit bitmaps? If not let me know, and i''ll change some code to load 8/24 bit bitmaps.

  #include <Stdio.h> //I prefer stdio over iostreamstruct Bitmap_S{ unsigned short SizeX, SizeY; unsigned long DataSize; unsigned short BitDepth; unsigned char *Data;};/*Returns: 0 on success 1 on file not found 2 on invalid BMP file 3 on not enough memory*/char LoadBitmap(char *FileName, Bitmap_S &Bmp){ short tx,ty; unsigned long offset; FILE *in; Bmp.Data=0; //Set data to null! in = fopen(FileName,"rb"); if (!in)  return 1; //File not found! if (getc(in)!=''B'' || getc(in)!=''M'')  //Check bitmap signature {  fclose(in);  return 2; } fseek(in,18,0); Bmp.Width = getw(in); fseek(in,22,0); Bmp.Height = getw(in); fseek(in,28,0); Bmp.BitDepth = getc(in); fseek(in,54,0); //Beginning of data! Bmp.DataSize = Bmp.Width * Bmp.Height * (Bmp.BitDepth>>3); Bmp.Data = new unsigned char[Bmp.DataSize]; if (!Bmp.Data) //Memory allocation go ok? {  fclose(in);  return 3; }/*  Bitmaps are stored upside down, and b,g,r.. so we have to flip the image vertically, AND swap colors while loading it..*/ for (ty=Bmp.Height-1;ty!=-1;--ty) //Loop backwards (upside down) {  offset = ty*Bmp.Width*Bmp.BitDepth;  for (tx=0;tx!=Bmp.Width;++tx)  {   Bmp.Data[offset+2]=getc(in); //Blue   Bmp.Data[offset+1]=getc(in); //Green   Bmp.Data[offset+0]=getc(in); //Red   offset+=3;  } } fclose(in); return 0;}void FreeBitmap(Bitmap_S &Bmp){ if (Bmp.Data)  delete Bmp.Data; Bmp.Width=Bmp.Height=Bmp.DataSize=0; Bmp.Data=0;}  


Alright, well, this only does 24-bit bitmaps for now, and i wrote this in this little text box, so it *may* contain gramatical errors, if you need help with it, or adding to it, let me know..

Billy - BillyB@mrsnj.com
Advertisement
ok, so i´m wrong. it was just a thought.
but what does ms means by:
"The new operator does not allocate reference types because they are not objects."
char and int are reference types, or is this my mistake?

greetings
Wow, thanks for all ur help.
oh ans sizeof can return the number of elemnets in an array just use: sizeof array / sizeof array[0]
i fink , look it up in the help files

oh and mr annon im trying to use streams not stdio
iostreams are easier to understand in my opinion
:D

it still doesnt work i dont fink the fin.read() funstion is being used properly...
fin.read((char*) &bmpData, sizeof(bmpData)*imageSize);
i get an EAccessviolation or sumething.. Borland C++ Compiler is not very good compared to VC++ as i said...

Wot Sig?


Edited by - CEdCopy on February 12, 2002 9:41:04 AM
Wot Sig?
>>i get an EAccessviolation or sumething.. Borland C++ Compiler is not very good compared to VC++ as i said<<

the problem lies in your code not with borland c++. whats that saying ''a poor workman blames his tools''

http://uk.geocities.com/sloppyturds/gotterdammerung.html
thx for your comment... and is very helpful to me
Wot Sig?

This topic is closed to new replies.

Advertisement