   c++ file IO vs c (and some other things)
  Hey, a while back I put together a quick and dirty (limited compatibility) targa loader for my game, using just standard C file io functions.Now, I decided to try and strip the SDL components out of Secret Maryo Chronicles, after deciding that SDL is stupid, and I've wanted to avoid the insane DLL requirements for SDL projects. Anyway, Maryo in particular uses a lot of PNG and jpeg images. So I've started to write my own PNG loader (I imagine the game in particular doesn't use any of the write features of pnglib anyway, sooo) but anyway, I've started with just the basics (so the picture won't actually display, or use any of the filters, but just load the data into a buffer period)ANWAY, so unlike my targa loader, I've used the fstream class of C++ instead, under the impression that it would be easier to weed out the chunk ID's (which turned out to be incredibly false, although I imagine there are better ways than what I'm doing now, which is just rediculous imho..)So, I can get the IHDR chunk, and most of the IDAT chunk.Problem is, I get a seg fault trying to read the big chunk of data.struct png_chunk{   unsigned int L[4];    // Chunk Length  char    T[4]; // Chunk Type};struct png_data{  png_chunk  X;   // Chunk information  unsigned char  *D; // Data  unsigned int    R;   // CRC};png_data get_data(fstream *file){ png_data A; A.X = get_chunk(file); A.D = new unsigned char[(int)A.X.L]; file->read((char *)&A.D, (int)A.X.L); // read Data field to the size of what it says in the chunk header //file->seekg((int)A.X.L , ios::cur); // skip the data chunk and go right to the CRC file->read((char *)&A.R, 4    ); // read  return A;}I tried skipping the data chunk to go right to the CRC to try and match them up, and they return different values :XByte alignment shouldn't be an issue, I don't think, because all the fields of all the structs are read individually... hmm.Another thing, in the IHDR chunk, the width and height come out to be 2147483648 each, they should be 128x128 in this test image, well this is the output:16  <-- tellp() after the last character of the chunk name (so i rewind 8 to get do the get_chunk() function which gets the chunk length and tag2293536 <--- lengthIHDR    <--- tagWidth:  2147483648Height: 2147483648Depth:  8Type:   2compression: 0Filter: 0Interlace:   0CRC:    2633391180whereas rewinding 9 characters instead i get...162293536 <--- length <-- same as above.. weird?IHDR    <--- tag    <-- also sameWidth:  82 <-- uh..  Height: 128Depth:  128Type:   8compression: 2Filter: 0Interlace:   0CRC:    4133243904so yeah, it looks like that's how it should be. 82 is of course the ascii integer for R, so I guess it's grabbing that one(two?) byte for Width... doesn't really make sense to me, since width should grab 4 byte value... PNG is weird lol.UHH oh yeah,also the rest of my question (sorry man i have major ADHD here)does my segmentation fault because of fstream, because in my targa loader, using the C file io functions, nothing like that ever happened, it just allocate space for the data and read to it.Well,  i think my IHDR problem is because that seekg() goes back in characters in the text right, so... the 4 bytes of the chunk size wouldn't be 4 characters necessarily right...   
  Well I seem to have figured it all out but thanks for all your input and support :Z   