C 語言實例 - 從文件中讀取一行


C 語言實例 - 從文件中讀取一行

從文件中讀取一行。 文件 runoob.txt 內容: $ cat runoob.txt runoob.com google.com
實例
#include
<stdio.h> #include <stdlib.h> // exit() 函數 int main() { char c[1000]; FILE *fptr; if ((fptr = fopen("runoob.txt", "r")) == NULL) { printf("Error! opening file"); // 文件指針返回 NULL 則退出 exit(1); } // 讀取文本,直到碰到新的一行開始 fscanf(fptr,"%[^\n]", c); printf("讀取內容:\n%s", c); fclose(fptr); return 0; }
輸出結果為: 讀取內容: runoob.com

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM