Thread: dont know how why this doesnt work

  1. #1
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101

    Question dont know how why this doesnt work

    throws exception when trying to use pointer to store unsigned chars in array
    Code:
    unsigned char texture1 = 2700000;// 262 144;
    the function :
    I am stuck with not finding a solution now after trying different thing for a while
    Code:
    int readbmp() {	
    	fileheader bmpheader;
    	infoheader bmpinfoheader;
    		unsigned char *p,*p2;
    		p =&texture1;
    		
    		unsigned char header[100];
    		int f, i, j,cnt;// buffer[10961024];
    		cnt = 0;
    	//char str[2000];
    	unsigned char c;
    	f = 2048;
    	
    	ifstream fileread("test.bmp",ios::binary);
    	//ifstream fileread("test.txt");
    	//read in 40 bytes header
    	cout << "fileheaders";
    	for (j = 0; j < (40+14); j++) {
    		fileread >> c;
    		header[j] = c;
    		i = (int)c; i = i & 0xff;
    		cout << i << "\n";
    	}
    	i = 0x001c;
    	if (header[i] = 24) {
    		cout << "24bit bmp ";
    		cout << "bitmapdata";
    		while (!fileread.eof()) {
    			fileread >> c;
    			(*p) = c;//exception always here
    			++p;
    			//cout << c;
    			
    			
    			f--;
    			//if (f == 0) break;
    		}
    
    
    	}
    	else {
    		cout << "8bit palette bmp \n";
    	}
    	fileread.close();
    
    
    	return 1;
    }
    you tell me you can C,why dont you C your own bugs?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think that you've accidently posted c++ on the C forum

    What is the maximum number that your system can store in an unsigned char?

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    unsigned char.... 8 bits value...

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by flp1969 View Post
    unsigned char.... 8 bits value...
    Then this isn't going to go well...
    Code:
    unsigned char texture1 = 2700000;

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    214
    Code:
     if (header[i] = 24)
    This is an assignment, not a test

    Code:
    (*p) = c;//exception always here
    
    ++p;
    Given @Click_here's point, I can't see how that would do anything else but fire an exception.

    'p' points to something that stores one byte, somehow expected to store a value requiring 32 bits or more.

    Maybe an array should be used?
    Last edited by Niccolo; 09-02-2019 at 09:04 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    * moved to C++ programming forum *

    You should check the return value of fileread >> c rather than assume it succeeds. You can use it to control the loops. Do not use eof() to control reading in a loop like you are doing now since you would then continue in the loop body even if fileread >> c fails.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    thanks everyone ,mistake to have declaration of texture1 far away and missed that
    is it not prefered to have dynamic memory allocation with big files better and safer than arrays?
    fileread or other function is possible to assign whole array after the 2 fileheaders are read in,instead of loop?

    maybe possible to have two file opened simultanously and merge 8bit+24bit to 32bit?
    Last edited by I C everything; 09-03-2019 at 08:59 AM.
    you tell me you can C,why dont you C your own bugs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me i dont know why this doesnt work
    By Mr.who in forum C Programming
    Replies: 6
    Last Post: 11-25-2011, 11:32 AM
  2. Why doesnt this work
    By Hugo716 in forum C++ Programming
    Replies: 13
    Last Post: 05-18-2006, 11:57 AM
  3. Why doesnt this work
    By digdug4life in forum C++ Programming
    Replies: 13
    Last Post: 06-19-2005, 03:22 PM
  4. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 04:55 PM
  5. why doesnt this work?
    By brad123 in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 05:26 PM

Tags for this Thread