Hi everybody.
I`m having an odd problem with my code and i`m out of ideas.
i`m using ubuntu 7.10 and eclipse with gcc.
i`m trying to read a .bmp file, here is the code:
this is the code for BMPFileAdministrator.h:Code:#include <stdio.h> #include <stdlib.h> #include "FifoList.h" #include "FileLayer.h" #include "CodeLayer.h" #include "CapaAscii.h" #include "BMPFileAdministrator.h" int main (int argc, char** argv){ FifoList* fifoListForLongitudes = createFifoList(); FifoList* fifoListForValues = createFifoList(); int fileLayerError = 0; int codeLayerError = 0; int* dataBuffer; int _i = 0 ; int _data; FILE* _bmpFile = fopen("hola.bmp", "r+b"); BMPData* bmpData = (BMPData*)malloc(sizeof(BMPData)); if (_bmpFile != NULL){ printf ("algo hago\n"); // aca abro el bmp errorDeArchivo = openBMPFile (_bmpFile, bmpData); if (!(feof(_bmpFile))){ fseek (_bmpFile, 18, SEEK_SET); fread (dataBuffer, 4, 1, _bmpFile); printf("%d\n", *dataBuffer); _data = *dataBuffer; printf("%d\n", _data >> 10); } while(_i < 5){ takeLongitudesFromFile(_bmpFile); _i++; } destroyFifoList (fifoListForLongitudes); destroyFifoList (fifoListForValues); free (bmpData); fclose (_bmpFile); } if (fileLayerError && codeLayerError && errorDeCapaAscii && errorDeArchivo){ return 1; }else{ //aca mostra el mensaje system("pause"); return 0; } }
[/CODE]Code:typedef struct BMPDataType{ int positionIndex; int height; int width; int startingAtByte; int headerSize; int padding; int rowTwentyStartsAtByte; } BMPData; int openBMPFile(FILE* bmpFile, BMPData* bmpData);
and here is the implementation fon openBMPFile:
im getting correct values from the code that reads the header of the file, it gets the data correctly. but the problem is that after that if i want to read any further i start getting segmentation fault for stupid things like trying to do a for loop to show on the console a piece of the file using something like thisCode:int openBMPFile(FILE* bmpFile, BMPData* bmpData){ int error = 0; char buffer[2]; int* dataBuffer; int fileSize = 0; //int i = 0; //int j; //int _data; //int h; if (!(feof(bmpFile))){ fread(buffer, 2, 1, bmpFile); if(buffer[0] == 'B' && buffer [1] == 'M'){ fread(dataBuffer, 4, 1, bmpFile); fileSize = *dataBuffer; //printf ("%d", fileSize); fseek (bmpFile, 10, SEEK_SET); fread(dataBuffer, 4, 1, bmpFile); bmpData->startingAtByte = *dataBuffer; printf("%d \n", *dataBuffer); fseek (bmpFile, 14, SEEK_SET); fread (dataBuffer, 4, 1, bmpFile); bmpData->headerSize = *dataBuffer; printf("%d \n", *dataBuffer); fseek (bmpFile, 18, SEEK_SET); fread (dataBuffer, 4, 1, bmpFile); bmpData->width = *dataBuffer; printf("%d \n", *dataBuffer); fseek (bmpFile, 22, SEEK_SET); fread (dataBuffer, 4, 1, bmpFile); bmpData->height = *dataBuffer; printf("%d \n", *dataBuffer); //padding expresado en bits bmpData->padding = 8*((fileSize - bmpData->startingAtByte)/bmpData->height) - bmpData->width; printf("%d \n", bmpData->padding); bmpData->rowTwentyStartsAtByte = (((bmpData->height - 20)*(bmpData->width + bmpData->padding))/8) + bmpData->headerSize; }else{ error = 1; } }else{ error = 1; } return error; }
even if i comment all the code inside the for it shows segmentation fault, i comment the whole for and no segmentation fault.Code:for (i = 0; i < bmpData->width; i = i +32){ fread (dataBuffer, 4, 1, bmpFile); for (j = 31; j > -1; j--){ _data = *dataBuffer; printf("%d", _data >> j); } }
i tried reading the file after it exits this method and i can read it but when i try to read the file in a while or a for loop i get the same problem, a segmentation fault. the most weird thing is that with this code in the main after the call to openBMPFIle
it reads fine but when i add afer this this code:Code:if (!(feof(_bmpFile))){ fseek (_bmpFile, 18, SEEK_SET); fread (dataBuffer, 4, 1, _bmpFile); printf("%d\n", *dataBuffer); _data = *dataBuffer; printf("%d\n", _data >> 10); }
this does not work, segmentation fault it says, but if i remove the code that uses the int i variable it works fine. meaning that the int i is the problem(?).Code:int i = 0; int b = 0; int* a; fseek(_bmpFile, b, SEEK_SET); fread(a, 4, 1, _bmpFile); for (j = 31; j > -1; j--){ _data = *a; //_data = _data << i; _data = _data >> j; printf("%d\n", _data ); i++; }
So this is the problem: i open a file, i read it just fine in a function up to some point and then when i trie to read it it starts trowing segmentations fault for weird things.
Thank you for your time, any opinions or help will be apreciated.



LinkBack URL
About LinkBacks


