Skip to content

Instantly share code, notes, and snippets.

@ruyut
Last active March 28, 2019 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruyut/bd354d4de2a23979ceba654ed0682bc5 to your computer and use it in GitHub Desktop.
Save ruyut/bd354d4de2a23979ceba654ed0682bc5 to your computer and use it in GitHub Desktop.
//904-1
#include<stdlib.h>
#include<stdio.h>
int main() {//r讀取 w寫入 a讀寫
FILE *fi;//宣告檔案指標
char name[20];//宣告字元陣列
int score;//宣告變數
fi = fopen("score.dat", "w");//使用寫入的方式開啟檔名為score.dat的檔案
do {
printf("請輸入學生的姓名(分數輸入負的分數時結束):");
scanf("%s", &name);//輸入字元,存在name字元陣列中
printf("請輸入此學生C語言分數:");
scanf("%d", &score);//輸入整數
if (score >= 0)//如果分數大於等於0
fprintf(fi, "%s %d", name, score);//把名字和分數輸出到檔案當中
} while (score >= 0);//在條件成立的情況下重複執行迴圈 (如果輸入的數字大於等於0)
fclose(fi);//關閉檔案
system("PAUSE");//等待使用者按下任意按鍵
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment