문제

    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.

도움이 되었습니까?

해결책

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!
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top