C 語言實例 - 將字串寫入檔

C 語言實例 C 語言實例

將字串寫入檔。

實例

#include <stdio.h> #include <stdlib.h> /* exit() 函數 */ int main() { char sentence[1000]; FILE *fptr; fptr = fopen("zaixian.txt", "w"); if(fptr == NULL) { printf("Error!"); exit(1); } printf("輸入字串:\n"); fgets(sentence, (sizeof sentence / sizeof sentence[0]), stdin); fprintf(fptr,"%s", sentence); fclose(fptr); return 0; }

輸出結果為:

輸入字串:
xuhuhu.com

打開檔 zaixian.txt:

$ cat zaixian.txt
xuhuhu.com

C 語言實例 C 語言實例