C 文件读写整理

feof

Checks whether the end-of-File indicator associated with stream is set, returning a value different from zero if it is.

  1 #include<stdio.h>
  2 #include<string.h>
  3 int main()
  4 {
  5     FILE *fp;
  6     char str[60];
  7     float f;
  8     int c, n=0;
  9 
 10     fp = fopen("file.txt", "w+");
 11     fprintf(fp, "%f %s", 3.1416, "PI");
 12     rewind(fp);
 13     //fscanf(fp, "%f %s", &f, str);
 14     fscanf(fp, "%f", &f);
 15     fscanf(fp, "%s", str);
 16 /* fgets */
 17 /*
 18     fp = fopen("file.txt", "r");
 19     if(fp == NULL) {
 20         perror("Error opening file");
 21         return (-1);
 22     }
 23      
 24     while( fgets(str, 60, fp) != NULL) {
 25         str[strlen(str)-1]='\0';
 26         puts(str);
 27     }
 28 */
 29     fclose(fp);
 30     printf("%f and %s \n", f, str);
 31 
 32     fp=fopen("file.txt", "r");
 33     if(fp == NULL)
 34         perror("Error opening file");
 35     else
 36         while((c = fgetc(fp)) != EOF)
 37           if(c == '0')
 38                 n++;
 39     fclose(fp);
 40     printf("n=%d\n", n);
 41     return 0;
 42 }
  1 #makefile
  2 #write at 20150519
  3 #write by walter
  4 
  5 CC=gcc
  6 CFLAGS=-g -Wall
  7 TARGETS=iotest
  8 
  9 all:$(TARGETS)
 10 
 11 iotest: iotest.o
 12 #   $(CC) $(CFLAG) -o fgets fgets.o
 13 
 14 .PHONY: clean
 15 clean:
 16     rm -rf $(TARGETS) *.o



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值