Pregunta

    FILE *dataScore;
    dataScore = fopen(fileName.dat, "w");
    fprintf(dataScore,"%s:%d\n",currentUser,score);
    fclose(dataScore);

The file crashes on the line printing to file. I believe it is due to the username but I may be wrong. Thanks in advance. Set the currentUser as 02heasam and score as 20.

¿Fue útil?

Solución

looks crazy...

try this way:

int score=20;

int main(void){   

    char* currentUser = "02heasam";

    FILE *dataScore;
    dataScore = fopen("fileName.dat", "w");

    fprintf(dataScore,"%s:%d\n",currentUser,score);
    fclose(dataScore);

}

some explanations:

  • to fill a char array with a string you would need strcpy or so. Not needed here!
  • the order might be important (declaration before usage)
  • a strimng literal "xxx" will automatically terminated by a trailing 0-Byte - NEVER miss to have this in mind!
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top