FILE *fopen( const char *filename, const char *mode );
int fclose( FILE *fp );
int fputc( int c, FILE *fp );
int fputs( const char *s, FILE *fp );
int fprintf(FILE *fp, const char *s);
int fgetc( FILE * fp );
char *fgets( char *buf, int n, FILE *fp );
char fscanf(FILE *fp, "%s",char *buff);
#include
#include
int main()
{
FILE *fp = NULL;
char buff[40];
fp = fopen("test.txt","w+");
int s = fprintf(fp, "xiaoma gede biji.....\n"); // 写入成功,返回字符的个数
int s1 = fputs("string......\n",fp); // 写入成功,返回非零值
char c = fputc('x',fp); // 写入成功返回写入的字符
int i = fclose(fp); // 关闭成功返回0
printf("i = %d\nc = %c\ns = %d\ns1 = %d\n",i, c, s, s1);
fopen("test.txt","r+");
char c_r = fgetc(fp); // 读取文件的第一个值,成功返回读取值,否则返回EOF(-1)
/*
fgetc和fgets会相互影响,fgetc会读取第一个字符,
fgets只能从剩下的地方进行读取,无法读取\n,文件末尾的EOF
*/
fgets(buff,8,fp); // 读取6-1个,最后追加一个null
printf("c_r = %c\nbuff = %s\n",c_r, buff);
fscanf(fp, "%s", buff); // 无法读取空格,换行符,EOF
printf("s_fscanf:%s",buff);
fclose(fp);
return 0;
}
i = 0
c = x
s = 22
s1 = 1
c_r = x
buff = iaoma g
s_fscanf:ede