Question

    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.

Was it helpful?

Solution

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!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top