C 文件读写整理

来源:互联网 发布:品牌营销找优爵网络 编辑:程序博客网 时间:2024/04/30 15:27

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



0 0
原创粉丝点击