Skip to content

Commit

Permalink
修正项目2 C语言代码bug
Browse files Browse the repository at this point in the history
1. record.txt 文件为空读取导致乱码;
2. 第一次玩记录没有名字;
  • Loading branch information
imlinhanchao committed Apr 12, 2018
1 parent bc95ef1 commit 4ec05fa
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions 02/backend/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ int loadNickname(const char* szPath, char* szNickname, int nSize)
return 0;
}

int setNickname(const char* szPath)
const char* setNickname(const char* szPath, char* szNickname)
{
FILE *fp = 0;
char szNickname[MAX_NAME_LEN] = "";
if (!(fp = fopen(szPath, "wb")))
return 1;
printf("请输入你的名字:");
scanf("%s", szNickname);
szNickname[MAX_NAME_LEN - 1] = 0;
fwrite(szNickname, sizeof(szNickname), 1, fp);
fclose(fp);
return 0;
return szNickname;
}

struct RECORD guess(char* szNickName)
Expand All @@ -66,13 +65,13 @@ struct RECORD guess(char* szNickName)
stRecord.nCount++;
}
stRecord.nCost = time(NULL) - stRecord.tBeginTime;
strcpy(stRecord.szName, szNickName);
printf("你猜中了!你猜了%d次!\n", stRecord.nCount);
if (0 == szNickName || 0 == strlen(szNickName))
{
printf("这是你第一次玩,");
setNickname(SETTING_PATH);
setNickname(SETTING_PATH, szNickName);
}
strcpy(stRecord.szName, szNickName);
return stRecord;
}

Expand Down Expand Up @@ -113,13 +112,13 @@ int view()

while(!feof(fp))
{
fscanf(fp, "%d,%d,%d,%ld,%d,%s\r\n",
if(fscanf(fp, "%d,%d,%d,%ld,%d,%s\r\n",
&stRecord.nRand,
&stRecord.nGuess,
&stRecord.nCount,
&ltime,
&stRecord.nCost,
stRecord.szName);
stRecord.szName) < 0) continue;
stRecord.tBeginTime = (time_t)ltime;
localtime_s(&stTime, &stRecord.tBeginTime);

Expand Down Expand Up @@ -162,7 +161,7 @@ int main()
{
case '1': save(guess(szNickname)); break;
case '2': view(); break;
case '3': setNickname(SETTING_PATH); break;
case '3': setNickname(SETTING_PATH, szNickname); break;
case '4': return 0;
default : continue;
}
Expand Down

0 comments on commit 4ec05fa

Please sign in to comment.