Posted on 2008-09-19 13:33
Prayer 閱讀(7044)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
C/C++
fgets函數(shù)的功能是從指定文件中讀取字符串。一般格式為:
char * fgets(char * s, int n, FILE * stream);
當(dāng)讀到n - 1個(gè)字符或換行符時(shí),fgets函數(shù)將停止操作。該函數(shù)在s的末尾保留換行符,s的末尾增加了一個(gè)NULL字符標(biāo)明串的結(jié)束。如果調(diào)用成功。將返回由指針s指向的串,如果遇到文件結(jié)束符或出錯(cuò),將返回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