C语言fgets函数了解
来源:互联网 发布:谁有呼死你软件免费版 编辑:程序博客网 时间:2024/06/08 05:09
转自http://blog.csdn.net/hgj125073/article/details/8283188
原型是: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:
- #include <stdio.h>
- int main(void)
- {
- FILE *stream;
- char line[23];
- if (fopen_s(&stream,"abc.txt","r")==0) // hello,world
- {
- if (fgets(line,4,stream) == NULL)
- {
- printf("fgets error \n");
- }
- else
- {
- printf("%s\n",line);
- //printf("len is %d\n",strlen(line));
- }
- fclose(stream);
- }
- system("pause");
- return 0;
- }
DEMO2:
- int main()
- {
- FILE *stream;
- char string[]="This is a test";
- char msg[20];
- /*w+打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。*/
- stream=fopen("abc.txt","w+"); /*open a file for update*/
- fwrite(string,strlen(string),1,stream); /*write a string into the file*/
- fseek(stream,0,SEEK_SET); /*seek to the start of the file*/
- fgets(msg,strlen(string)+1,stream);
- printf("%s",msg);
- fclose(stream);
- system("pause");
- return 0;
- C语言fgets函数了解
- C语言fgets函数了解
- C语言fgets函数了解
- c语言fgets函数
- c语言中的fgets函数
- C语言中的fgets函数
- C语言函数原型fgets fputs
- 【C语言】-->语法 fgets函数原理初探
- C语言文件读函数fgets()
- C语言中fgets()函数
- C语言的字符串输入fgets()函数
- C语言 fgets函数与fputs函数用法
- C语言 fgets函数与fputs函数用法
- C语言字符串与文件读写函数 fgets(),fputs()......
- C语言中gets、fgets以及fputs函数
- c语言 fopen,fgets,fputc,putchar函数的简单使用
- C语言中gets是危险函数,用fgets代替
- C语言字符串读取函数 gets()与fgets()
- another brownian motion - analysis
- ppt转pdf转换器绿色版效果如何
- 搞明白OAuth 2.0是啥
- jquery file upload报错:Cannot call method 'parseMetaData' of undefined
- Oracle树查询
- C语言fgets函数了解
- ios知识点总结——富文本实现图文混排
- JAVA基本配置
- HDU1203 I NEED A OFFER! (dp)
- 仿IOS桌面图标右上角显示消息数目,或日历更新日期原理
- SimpleAdapter中ViewBinder 使用
- Struts HTML 标签库
- Mahout-DistanceMeasure (相似度计算)
- 非空的代码