Author Topic: (SOLVED) fread() is driving me mad. please help  (Read 1669 times)

0 Members and 1 Guest are viewing this topic.

Offline georgianTopic starter

  • Regular Contributor
  • *
  • Posts: 61
  • Country: at
(SOLVED) fread() is driving me mad. please help
« on: September 05, 2023, 08:04:10 pm »
Hello guys,

something mad is happenning in line 79 of my code.
Code: [Select]
LINE 78: printf("%d\n", pixelX); //Prints 0
LINE 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 bleed :palm:

Code: [Select]
/*
    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          //27C256

uint8_t ch2 = 0b01101001;   //0x69
uint8_t ch1 = 0b10011111;   //0x9F

uint8_t romCH1[romSize];    //Lower Byte
uint8_t romCH2[romSize];    //Upper Byte
uint16_t buffer[romSize];   //Lower and Upper combined.

uint8_t var, pixelX, pixel1, pixel2, pixel3 = 0;
//uint8_t var;

uint8_t px[4];

#define PI 3.14

uint32_t fileLen;
FILE *binary;

COLORREF palette[16] = {
    RGB(0x00, 0x00, 0x00),  //Black         #0
    RGB(0xDE, 0xEB, 0xB8),  //Cream         #1
    RGB(0xFF, 0xA0, 0x97),  //Pink          #2
    RGB(0xFF, 0xA0, 0x97),  //Pink          #3
    RGB(0xFF, 0x00, 0x00),  //Red           #4
    RGB(0x00, 0x00, 0x00),  //Black         #5
    RGB(0x00, 0xA0, 0x00),  //Dark Green    #6
    RGB(0x00, 0x00, 0xDE),  //Some Blue     #7
    RGB(0x47, 0xEB, 0x47),  //Light Green   #8
    RGB(0x00, 0xEB, 0xFF),  //Sky Blue      #9
    RGB(0xFF, 0xEB, 0xFF),  //Dirty White   #A
    RGB(0xFF, 0x00, 0x00),  //Red           #B
    RGB(0x47, 0x4B, 0x47),  //Gray          #C
    RGB(0x47, 0xEB, 0x97),  //Unseen Green  #D
    RGB(0x00, 0x00, 0x00),  //Black         #E
    RGB(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 0
    fread(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[romSize]);

//    for (uint32_t p = 0; p <= romSize; p++) {
//        buffer[p] = (romCH2[p] << 8) | romCH1[p];
//        //printf("%X ", romCH1[p]);
//        //if (p%4 == 0)
//          //  printf("\n");
//    }
    //printf("\n\nBuffer:%X\n", buffer[romSize]);

    printf("CH1:0x%X CH2:0x%X\nPixel[0] HEX:0x%X DEC:%d\n\n", ch1, ch2, pixelX, pixelX);
    printf("CH1:0x%X CH2:0x%X\nPixel[1] HEX:0x%X DEC:%d\n\n", ch1, ch2, pixel1, pixel1);
    printf("CH1:0x%X CH2:0x%X\nPixel[2] HEX:0x%X DEC:%d\n\n", ch1, ch2, pixel2, pixel2);
    printf("CH1:0x%X CH2:0x%X\nPixel[3] HEX:0x%X DEC:%d\n\n", ch1, ch2, pixel3, pixel3);

    uint8_t pp = getPixelColor(0, 0, romCH1[0], romCH2[0]);

    printf("Pixel:%X\n", pp);

    //Get a console handle
    HWND myconsole = GetConsoleWindow();
    //Get a handle to device context
    HDC mydc = GetDC(myconsole);

    int pixel =0;

    //Choose any color
    COLORREF COLOR= RGB(0,255,0);

    //Draw pixels
    for(double i = 0; i < PI * 4; i += 0.05)
    {
        SetPixel(mydc,pixel,(int)(50+25*cos(i)),palette[0x9]);
        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;
}


« Last Edit: September 05, 2023, 08:24:45 pm by georgian »
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 5830
  • Country: dk
Re: fread() is driving me mad. please help
« Reply #1 on: September 05, 2023, 08:10:24 pm »
how long is the file?
 
The following users thanked this post: georgian

Offline georgianTopic starter

  • Regular Contributor
  • *
  • Posts: 61
  • Country: at
Re: fread() is driving me mad. please help
« Reply #2 on: September 05, 2023, 08:11:46 pm »
how long is the file?
27C256 eprom dump
fileLen prints as 32768
« Last Edit: September 05, 2023, 08:13:27 pm by georgian »
 

Offline georgianTopic starter

  • Regular Contributor
  • *
  • Posts: 61
  • Country: at
Re: fread() is driving me mad. please help
« Reply #3 on: September 05, 2023, 08:23:45 pm »
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. :-DD

Thanks for pointing that out.
Code: [Select]
#define romSize     (0x7fff + 1)          //27C256
 

Offline magic

  • Super Contributor
  • ***
  • Posts: 8070
  • Country: pl
Re: (SOLVED) fread() is driving me mad. please help
« Reply #4 on: September 05, 2023, 08:36:03 pm »
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 and :wtf: again.
 
The following users thanked this post: georgian

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6928
  • Country: es
Re: (SOLVED) fread() is driving me mad. please help
« Reply #5 on: September 05, 2023, 08:40:42 pm »
This is PC, so just get the file size and allocate the buffer dynamically.
Code: [Select]
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: [Select]
free(buf);

Same for the other buffers.
« Last Edit: September 05, 2023, 08:46:22 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: georgian

Offline georgianTopic starter

  • Regular Contributor
  • *
  • Posts: 61
  • Country: at
Re: (SOLVED) fread() is driving me mad. please help
« Reply #6 on: September 05, 2023, 08:50:26 pm »
This is PC, so just get the file size and allocate the buffer dynamically.
Code: [Select]
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: [Select]
free(buf);

Same for the other buffers.

The buffer should be globaly so I can access it from other functions.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6928
  • Country: es
Re: (SOLVED) fread() is driving me mad. please help
« Reply #7 on: September 05, 2023, 09:01:39 pm »
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: [Select]
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);  //Done
  buf=NULL;  // Good practice to clear the pointer after freeing it up.
}

int main(void){
  if(allocate_buffer())
    error();
  else
    process_buffer();
}
« Last Edit: September 05, 2023, 09:06:45 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: georgian


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->