IO进程线程学习day1
时间:2023-06-17 02:07:00
测试结果
ymp@ubuntu:~/px/IO/fp$ ./a.out 2.txt input line>>> 3 qwr asf zxc 1.txt size:12,line:3 1.txt size:12,line:3 ymp@ubuntu:~/px/IO/fp$ cat 1.txt qwr asf zxc ymp@ubuntu:~/px/IO/fp$ cat 2.txt qwr asf zxc ///自己输入1.txt复制到2.txt中,利用两种函数求文件的大小和行数
.c文件
include #include #include #include"a.h" void copy(FILE *fp,FILE *fq) //文件复制函数 { char str[2]; rewind(fp); while(fgets(str,sizeof(str),fp) != NULL) { fputs(str,fq); } } int size_fgets(FILE *fp) //利用fgets求文件大小 { int sum=0; char str[20]; rewind(fp); while(fgets(str,sizeof(str),fp)!=NULL) { sum =strlen(str); } return sum; } int line_fgets(FILE *fp) //利用fgets求文件行数 { int line=0; char str[20]; rewind(fp); while(fgets(str,sizeof(str),fp)!=NULL) { int i; for(i=0;i
main.c函数
#include #include #include"a.h" int main(int argc,const char *argv[]) { FILE *fp=fopen("./1.txt","w "); if(NULL==fp) { printf("errno=%d\n",errno); perror("fopen"); return -1; } int i=0; int line; printf("input line>>> "); scanf("%d",&line); char str[20]; while(i
.h文件
#ifndef __fp__ #define __fp__ void copy(FILE *fp,FILE *fq); int size_fgetc(FILE *fp); int line_fgetc(FILE *fp); int size_fgets(FILE *fp); int line_fgets(FILE *fp); #endif ~ ~