My problem could also translate to stopping a buffer from overflowing;
1st strategy
char first[40];
char g[] = " Level %d %d Fruit %d:%d ";
char d[41] = {0};
strncpy(d, g, 40);
if(strlen(d) < 40) {
for(i = strlen(d); i < 40; i++) {
d[i] = ' ';
}
}
n = sprintf(first, d, gol, gift, minutes, secs );
I Print my n onto the game's screen, I get my text and some unknown characters following, which means there is a buffer overflow somewhere.
2nd strategy;
char first[40];
char b[40];
strncpy(b, g, sizeof(b));
fgets(b, 40, stdin); // of b[40] = '\0' also fails
n = sprintf(first, b, gol, gift, minutes, secs );
using " b[40] = '\0' ", I Print my n onto the game's screen, I get my text and some unknown characters following, which means there is a buffer overflow somewhere. using " fgets " crashes the game right away, even if g is 40 characters as of now, yet i want to increase it later
For both strategies, If I actually just put "g" in the " sprintf " statement, it does what I want except when I increase it to more than 40 characters. Sorry that I can't post the whole game code because its 4000 lines long split in 10 files.
I wold appreciate a completely different way of getting a string of only 40 characters from a string of more characters without leaving junk. The string has to be manually typed into the code, not input from user.
THANKS
Aucun commentaire:
Enregistrer un commentaire