Thread: fread problems or memory problems

  1. #1
    Registered User
    Join Date
    Mar 2009

    fread problems or memory problems

    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:

    #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){ 
    		destroyFifoList (fifoListForLongitudes);
    		destroyFifoList (fifoListForValues);
    		free (bmpData);
    		fclose (_bmpFile);
    	if (fileLayerError && codeLayerError && errorDeCapaAscii && errorDeArchivo){
    		return 1;
    		//aca mostra el mensaje
            return 0;	
    this is the code for BMPFileAdministrator.h:

    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:

    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;  					
    			error = 1;
    		error = 1;
    	return error;	
    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 this

    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);	
    even if i comment all the code inside the for it shows segmentation fault, i comment the whole for and no segmentation fault.

    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

    if (!(feof(_bmpFile))){
    			fseek (_bmpFile, 18, SEEK_SET);
    			fread (dataBuffer, 4, 1, _bmpFile);
    			printf("%d\n", *dataBuffer);
    			_data = *dataBuffer;
    			printf("%d\n", _data >> 10);
    it reads fine but when i add afer this this 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 );	
    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(?).

    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.

  2. #2
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    fread(a, 4, 1, _bmpFile);

    your a is not initialized pointer - you have no right to write there anything

    also - do not use feof to control loop - read FAQ
    To be or not to be == true

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. fread problems
    By Happy_Reaper in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 09:27 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. stl::vector problems (memory?)
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2003, 11:21 AM
  5. Tile Engine Problems with Video Memory
    By Tommaso in forum Game Programming
    Replies: 8
    Last Post: 03-07-2003, 07:26 PM
Website Security Test