c文件讀取之fgets

函數原型

char* fgets(char* s, int size, FILE* fp)


用法

函數會讀取最多size大小的數據到s中,當遇到EOF或者新行時也會停止,並將換行符“\n”保存在s中。


示例代碼:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 void main()
  4 {
  5   FILE *fp = fopen("test.in","r");
  6   char in[100],in2[100];
  7   fgets(in,sizeof(in),fp);
  8   fgets(in2,sizeof(in2),fp);
  9   printf("%s%s",in,in2);
 10 }


test.in


  1 this is a test!
  2 hello world!
  3 god bless me!
  4


運行結果:

this is a test!
hello world!

發佈了6 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章