C语言fgets函数了解
原型是:char *fgets(char *s, int n, FILE *stream);
從文件指針stream中讀取n-1個字符,存到以s為起始地址的空間里,直到讀完一行,如果成功則返回s的指針,否則返回NULL。
例如:一個文件是hello,world,
用fgets(str1,4,file1); ?
執行后str1="hel",讀取了4-1=3個字符.
而如果用而如果用fgets(str1,23,file1);
則執行str1="hello,world",讀取了一行(包括行尾的'\n',并自動加上字符串結束符'\0')。
The?fgets?function reads a string from the input?stream?argument and stores it in?str.?fgets?reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to?n?– 1, whichever comes first. The result stored in?str?is appended with a null character. The newline character, if read, is included in the string. ? ----from MSDN
DEMO1:
[cpp]?view plaincopy
DEMO2:
[cpp]?view plaincopy
總結
以上是生活随笔為你收集整理的C语言fgets函数了解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Countries
- 下一篇: 顺序表应用5:有序顺序表归并