I basically want to have an arbitrary length string but a fixed size output string. So if I assign x a string of 10 characters, I want to end up with a string of 5 characters at the second printf. And if x is 3 characters, I wanna add on blanks to make the string 5 characters. Here is my code so far, but its not working because I can't even access an index, it segfaults;
int main()
{
char x[] = "d dddddddd";
printf(x);
printf("");
// separate
printf(x[1]);
return 0;
}
Thanks, I wonder whether this is possible in C, but you could also attempt it in C++
EDIT: My 2 codes are here, they both overflow;
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 );
2nd strategy;
char first[40];
char b[40];
strncpy(b, g, sizeof(b));
fgets(b, 40, stdin);
n = sprintf(first, b, gol, gift, minutes, secs );
Print my n onto the game's screen, I get my text and some unknown characters following. 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.
Aucun commentaire:
Enregistrer un commentaire