0

I seem to have a syntax error on the line

FSFILE *file; 

in the following code after adding the sprintf() line. The code worked up until i added the char text, textresult and sprintf(). I can't seem to find out what's wrong with it. I'm using the C18 compiler. the code is for writing data to an SD card using SPI. the char txt[] is a value from a measurement with a temperature sensor, for example: 23,5. But I want to add more text to this. The goal is to store every 5 minutes a measurement on the SD card, along with a timestamp or something. i'm using a PIC18f27j53.

void writeFile()
{
    unsigned char txt[]={ftc(result,0),ftc(result,1),0x2C,ftc(result,3)};
    unsigned char text[]= "hello";  
    unsigned char textresult[];   
    sprintf(textresult, "%c%c", txt, text); 
    //unsigned char size = sizeof(result)-1;
    FSFILE *file;
    file = FSfopenpgm("DATA.TXT", "w");
    if(file == NULL)while(1);
    if(FSfwrite((void *) txt, 1, 4, file)!=4)while(1);
    if(FSfclose(file)!=0)while(1);
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    Is `sprintf()` really relevant? Doesn't it just mean that your compiler doesn't support declareing variables in the middle of function after writing some non-declaretion statements? – MikeCAT Mar 05 '18 at 14:57
  • 1
    `unsigned char textresult[]; sprintf(textresult, "%c%c", txt, text); ` undefined behaviour!!! because `textresult` isn't allocated!! – Jean-François Fabre Mar 05 '18 at 14:58
  • `textresult` is an empty array, how can you write soomething in an empty array? Also `txt` is a pointer, `%c` expects a `char` – Pablo Mar 05 '18 at 15:14
  • Isn't PIC18 an 8-bitter? Why are you using sprintf on a 8 bit MCU? It is not a PC - you just blew up the whole memory map. If you need to use such a resource-heavy function just to copy 2 strings, which you even fail to do correctly, there's no hope for this project. Hand it over to an embedded systems programmer instead. – Lundin Mar 05 '18 at 15:30

2 Answers2

0

Move the sprintf(...) after where you declare your variables.

cleblanc
  • 3,678
  • 1
  • 13
  • 16
0

I have no idea what ftc does but your txt might not be '\0'-terminated, if you want to use it as a string, it has to be '\0'-terminated.

Also your textresult is an empty array, what do you expect will happen if you try to write stuff where there is not space available?

unsigned char textresult[20];

would be correct.

Also note that %c in printf expects a single char value, you are passing a pointer to a whole sequence of chars, that's undefined behavior. You have to either use %s (and for this txt must be '\0'-terminated) or you pass txt[0], a single char:

sprintf(textresult, "%c%c", txt[0], text); 

// or

unsigned char txt[]={ftc(result,0),ftc(result,1),0x2C,ftc(result,3), 0};
...
sprintf(textresult, "%s%c", txt, text); 

And if you compiler want all variables to be declare at the beginning of the function, move the

FSFILE *file;

before the sprintf call.

Pablo
  • 13,271
  • 4
  • 39
  • 59