[MSX-C] Q&A official thread

Page 28/57
21 | 22 | 23 | 24 | 25 | 26 | 27 | | 29 | 30 | 31 | 32 | 33

By AxelStone

Prophet (3120)

AxelStone's picture

19-02-2016, 12:41

raymond wrote:

Is there also an easy to use TP version of this replayer?

Cool, there are TP programmers in the forum Smile . I should suggest open a similar thread for TP called "[Turbo Pascal] Q&A official thread" in order to centralize all questions about TP. Personally is a language that I like very much, I was hesitating between TP and MSX-C. Finally decided for MSX-C beacuse nowadays I see an interesting comunity behind it, but it should be great to see that there is also a TP comunity working Smile

By AxelStone

Prophet (3120)

AxelStone's picture

19-02-2016, 18:24

Hi again, I've continued with MSX-C and I've a problem reading text files. I've a text file like:

image001.sc5
000,000,100,500
Lorem ipsun

I'd like to read every line of the text file and store it in a string. However, it seems as like strings has some kind of thrash since they are not clear. Here goes the function:

char image[11];
char coords[14];
file=fopen("myfile.txt","r");
fread(image,sizeof(char),12,file);
getc(file); /* to skip \n */
fread(coords,sizeof(char),15,file);
getc(file);

When I prints both variables I get something like:
image001.sc5
000,000,100,050N1 <- ¿¿¿????

The second string has bad stuff. What could be happening? Thanks.

By AxelStone

Prophet (3120)

AxelStone's picture

19-02-2016, 18:41

Ok, it's my fault, I almost forgot: in C the character '\0' indicates "end of string, so you need to do:

char image[12];
...fread...
image[12]='\0';

It works now.

By AxelStone

Prophet (3120)

AxelStone's picture

21-02-2016, 13:52

Another question: how do I convert a readed string into NAT type. For example, I read something like:

0x0442

I'd like to convert it to NAT type in order to use it to set a colour of palette. How can I make de conversion?

Thanks.

By Grauw

Ascended (10604)

Grauw's picture

21-02-2016, 14:08

NAT type? What’s that?

Palette colours are set in pairs of bytes 0xRB, 0x0G (R=red, B=blue, G=green, see Accessing the Palette Registers section). Read as a little endian word, this gives you the value 0x0GRB. So 0x0442 means G = 4, R = 4, B = 2.

By AxelStone

Prophet (3120)

AxelStone's picture

21-02-2016, 14:43

Nat means unsigned, in c you can set 1 colour by setting it with a nat value (in the first 4 pages of this thread there is an example). It is grbdat as you say, but id like to convert from a readed string to nat, if possible.

By AxelStone

Prophet (3120)

AxelStone's picture

22-02-2016, 17:36

I almost have it, but I have the last obstacle: I don't know how to read a binary file with hex values. The .pl5 file has values as following:
FE 80...

Suposse I simply want to read first byte: how in the hell it's supossed I can read it in hexadecimal format? I've tried fread, fscanf, int values, int pointers...but there is no luck.

Thanks.

By raymond

Hero (606)

raymond's picture

22-02-2016, 20:29

AxelStone wrote:

Cool, there are TP programmers in the forum Smile . I should suggest open a similar thread for TP called "[Turbo Pascal] Q&A official thread" in order to centralize all questions about TP. Personally is a language that I like very much, I was hesitating between TP and MSX-C. Finally decided for MSX-C beacuse nowadays I see an interesting comunity behind it, but it should be great to see that there is also a TP comunity working Smile

Thanks, I will do Smile

By Grauw

Ascended (10604)

Grauw's picture

22-02-2016, 21:06

AxelStone: Maybe only block I/O is supported? At least this is the case in MSX-DOS. This means you have to read into a byte array or malloced buffer first, and then you can access the data in the buffer with pointers and casts.

By Sylvester

Hero (563)

Sylvester's picture

22-02-2016, 21:06

The only thing I can think of is:

#include <stdio.h>
int buf[1];

main(argc, argv)
int argc;
char *argv[];
{
  FILE *fp;
  fp = fopen(argv[1], "r");
  while(fread(buf, sizeof(int), 1, fp)) {
    printf("%5x", buf[0]);
  }
  fclose(fp);
}

it displays the file in hex codes, but only in pairs E4E4 etc..

Page 28/57
21 | 22 | 23 | 24 | 25 | 26 | 27 | | 29 | 30 | 31 | 32 | 33