(SOLVED) fread() is driving me mad. please help - Page 1
Forum section: electronics / projects

author: georgian
author_rank: Regular Contributor
timestamp: September 05, 2023, 08:04:10 pm
content: Hello guys,something mad is happenning in line 79 of my code.Code:LINE 78: printf("%d\n", pixelX); //Prints 0LINE 79: fread(romCH1, fileLen, 1, binary);LINE 80: printf("%d\n", pixelX); //prints 170??? why?The code is very very messy but that shouldn't be the problem....the variable pixelX (used to be pixel0) is initialized as uint8_t and it stays 0 until the fread() function is used. After that it suddenly becoms 170. I have no idea why and it sort of driving me mad. The code is under havy development and tests and also poor, sorry if it makes your eyes bleedCode:/*https://stackoverflow.com/questions/12378642/c-pixels-in-console-window*/#include <stdint.h>#include <stdio.h>#include <windows.h>#include <math.h>#define romSize 0x7fff //27C256uint8_t ch2 = 0b01101001; //0x69uint8_t ch1 = 0b10011111; //0x9Fuint8_t romCH1; //Lower Byteuint8_t romCH2; //Upper Byteuint16_t buffer; //Lower and Upper combined.uint8_t var, pixelX, pixel1, pixel2, pixel3 = 0;//uint8_t var;uint8_t px;#define PI 3.14uint32_t fileLen;FILE *binary;COLORREF palette = {RGB(0x00, 0x00, 0x00), //Black #0RGB(0xDE, 0xEB, 0xB8), //Cream #1RGB(0xFF, 0xA0, 0x97), //Pink #2RGB(0xFF, 0xA0, 0x97), //Pink #3RGB(0xFF, 0x00, 0x00), //Red #4RGB(0x00, 0x00, 0x00), //Black #5RGB(0x00, 0xA0, 0x00), //Dark Green #6RGB(0x00, 0x00, 0xDE), //Some Blue #7RGB(0x47, 0xEB, 0x47), //Light Green #8RGB(0x00, 0xEB, 0xFF), //Sky Blue #9RGB(0xFF, 0xEB, 0xFF), //Dirty White #ARGB(0xFF, 0x00, 0x00), //Red #BRGB(0x47, 0x4B, 0x47), //Gray #CRGB(0x47, 0xEB, 0x97), //Unseen Green #DRGB(0x00, 0x00, 0x00), //Black #ERGB(0x00, 0x00, 0x00), //Black #F};uint8_t getPixelColor(uint32_t adress, uint8_t pixelPos, uint8_t lowByte, uint8_t highByte);int main(){pixel1 = ((ch2 >> 5) & 1) << 3;pixel1 |= ((ch2 >> 1) & 1) << 2;pixel1 |= ((ch1 >> 5) & 1) << 1;pixel1 |= ((ch1 >> 1) & 1);pixel2 = ((ch2 >> 6) & 1) << 3;pixel2 |= ((ch2 >> 2) & 1) << 2;pixel2 |= ((ch1 >> 6) & 1) << 1;pixel2 |= ((ch1 >> 2) & 1);pixel3 = ((ch2 >> 7) & 1) << 3;pixel3 |= ((ch2 >> 3) & 1) << 2;pixel3 |= ((ch1 >> 7) & 1) << 1;pixel3 |= ((ch1 >> 3) & 1);binary = fopen("jolycard.ch1", "rb");//if (!binary) { printf("ERROR!\n");}fseek(binary, 0, SEEK_END);fileLen = ftell(binary);rewind(binary);printf("%d\n", pixelX); //Prints 0fread(romCH1, fileLen, 1, binary);printf("%d\n", pixelX); //prints 170??? why?fclose(binary);binary = fopen("jolycard.ch2", "rb");if (!binary) {printf("ERROR!\n");system("cls");}fseek(binary, 0, SEEK_END);fileLen = ftell(binary);rewind(binary);fread(romCH2, fileLen, 1, binary);fclose(binary);//printf("\n\nBuffer:%X\n", romCH2);// for (uint32_t p = 0; p <= romSize; p++) {// buffer = (romCH2 << 8) | romCH1;// //printf("%X ", romCH1);// //if (p%4 == 0)// // printf("\n");// }//printf("\n\nBuffer:%X\n", buffer);printf("CH1:0x%X CH2:0x%X\nPixel HEX:0x%X DEC:%d\n\n", ch1, ch2, pixelX, pixelX);printf("CH1:0x%X CH2:0x%X\nPixel HEX:0x%X DEC:%d\n\n", ch1, ch2, pixel1, pixel1);printf("CH1:0x%X CH2:0x%X\nPixel HEX:0x%X DEC:%d\n\n", ch1, ch2, pixel2, pixel2);printf("CH1:0x%X CH2:0x%X\nPixel HEX:0x%X DEC:%d\n\n", ch1, ch2, pixel3, pixel3);uint8_t pp = getPixelColor(0, 0, romCH1, romCH2);printf("Pixel:%X\n", pp);//Get a console handleHWND myconsole = GetConsoleWindow();//Get a handle to device contextHDC mydc = GetDC(myconsole);int pixel =0;//Choose any colorCOLORREF COLOR= RGB(0,255,0);//Draw pixelsfor(double i = 0; i < PI * 4; i += 0.05){SetPixel(mydc,pixel,(int)(50+25*cos(i)),palette);pixel+=1;}ReleaseDC(myconsole, mydc);return 0;}uint8_t getPixelColor(uint32_t adress, uint8_t pixelPos, uint8_t lowByte, uint8_t highByte) {// pixel0 = (((ch2 >> 4) & 1) << 3) | ((ch2 & 1) << 2) | (((ch1 >> 4) & 1) << 1) | (((ch1 >> 4) & 1) << 1) | (ch1 & 1);//// pixel1 = (((ch2 >> 5) & 1) << 3) | (((ch2 >> 1) & 1) << 2) | (((ch1 >> 5) & 1) << 1) | ((ch1 >> 1) & 1);////// pixel2 = (((ch2 >> 6) & 1) << 3) | (((ch2 >> 2) & 1) << 2) | (((ch1 >> 6) & 1) << 1) | ((ch1 >> 2) & 1);// pixel2 |= ((ch2 >> 2) & 1) << 2;// pixel2 |= ((ch1 >> 6) & 1) << 1;// pixel2 |= ((ch1 >> 2) & 1);//// pixel3 = (((ch2 >> 7) & 1) << 3) | (((ch2 >> 3) & 1) << 2) | (((ch1 >> 7) & 1) << 1) | ((ch1 >> 3) & 1);// pixel3 |= ((ch2 >> 3) & 1) << 2;// pixel3 |= ((ch1 >> 7) & 1) << 1;// pixel3 |= ((ch1 >> 3) & 1);switch(pixelPos) {case 0:{return (((highByte >> 4) & 1) << 3) | ((highByte & 1) << 2) | (((lowByte >> 4) & 1) << 1) | (lowByte & 1);break;}case 1: {return (((highByte >> 5) & 1) << 3) | (((highByte >> 1) & 1) << 2) | (((lowByte >> 5) & 1) << 1) | (((lowByte >> 1) & 1));break;}case 2: {return (((highByte >> 6) & 1) << 3) | (((highByte >> 2) & 1) << 2) | (((lowByte >> 6) & 1) << 1) | ((lowByte >> 2) & 1);break;}case 3: {return (((highByte >> 7) & 1) << 3) | (((highByte >> 3) & 1) << 2) | (((lowByte >> 7) & 1) << 1) | ((lowByte >> 3) & 1);break;}default: {return 0xFF;break;}}return 0;}

author: langwadt
author_rank: Super Contributor
timestamp: September 05, 2023, 08:10:24 pm
content: how long is the file?

author: georgian
author_rank: Regular Contributor
timestamp: September 05, 2023, 08:11:46 pm
content: Quote from: langwadt on September 05, 2023, 08:10:24 pm27C256 eprom dumpfileLen prints as 32768

author: georgian
author_rank: Regular Contributor
timestamp: September 05, 2023, 08:23:45 pm
content: Thank you @langwadt!I found the bug, array was to small. Changed the array size and pixelX stays at 0. I forgot abaout the leading \0 in an array... What a relieve.Thanks for pointing that out.Code:#define romSize (0x7fff + 1) //27C256

author: magic
author_rank: Super Contributor
timestamp: September 05, 2023, 08:36:03 pm
content: You may want to add a check for this after reading the file size.Sooner or later you will run this code on a different (larger) file andagain.

author: DavidAlfa
author_rank: Super Contributor
timestamp: September 05, 2023, 08:40:42 pm
content: This is PC, so just get the file size and allocate the buffer dynamically.Code:uint8_t * buf = NULL;fileLen = ftell(binary);buf = malloc(fileLen );if(!buf){printf("Unable to allocate file buffer!\r\n");error();}Just remember to free the buffer when you're done with it:Code:free(buf);Same for the other buffers.

author: georgian
author_rank: Regular Contributor
timestamp: September 05, 2023, 08:50:26 pm
content: Quote from: DavidAlfa on September 05, 2023, 08:40:42 pmThe buffer should be globaly so I can access it from other functions.

author: DavidAlfa
author_rank: Super Contributor
timestamp: September 05, 2023, 09:01:39 pm
content: It will be accesibe by anything if you declare a global pointer.You can do the malloc anywhere, the pointer and the data will be valid until you free it up.Just always check the buffer is valid (Not NULL) before accessing it.Code:uint8_t buf;int allocate_buffer(void){(...)fileLen = ftell(binary);buf=malloc(fileLen);return(buf==NULL); // Always check for valid pointer. If NULL, buffer allocation failed, return 1.}int process_buffer(void){if(buf==NULL) // It won't harm to check again the pointer is valid. As the program grows, new bugs are introduced, this might save your a$$ :)return 1;( do whatever with buffer )free(buf); //Donebuf=NULL; // Good practice to clear the pointer after freeing it up.}int main(void){if(allocate_buffer())error();elseprocess_buffer();}