Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

c函数:fgets()

Posted on 2008-09-19 13:33 Prayer 阅读(7030) 评论(0)  编辑 收藏 引用 所属分类: C/C++

fgets函数的功能是从指定文件中读取字符串。一般格式为:
char * fgets(char * s, int n, FILE * stream);
当读到n - 1个字符或换行符时,fgets函数将停止操作。该函数在s的末尾保留换行符,s的末尾增加了一个NULL字符标明串的结束。如果调用成功。将返回由指针s指向的串,如果遇到文件结束符或出错,将返回EOF。
#include <string.h>
#include <stdio.h>

main()
{
FILE * stream;
char string[] = "This is a test program!";
char msg[20];
stream = fopen("my.fil", "w+");
fwrite(string, strlen(string), 1, stream);
fseek(stream, 0, SEEK_SET);
fgets(msg, strlen(string) + 1, stream);
printf("%s", msg);
fclose(stream);
}

文章出处:http://www.diybl.com/course/3_program/c++/cppsl/200899/141237.html


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理