1.使用fgetc和fputc复制一个文件,例如.c将内容复制到2.c
void copy_file(FILE *fp, FILE *fq) //fp复制文件地址,fq复制文件地址 { int c = 0;//接收fgetc函数的返回值 while(1) { c = fgetc(fp); if(c<//文件结束时跳出循环 { break; } fputc(c, fq);///写入字符cfq } }
2.使用fgetc计算文件的大小,并将其包装成函数
int size_file(FILE *fp)//计算文件大小 { int size = 0;//大小初始化 while(1) { if(fgetc(fp)<0) { break; } size ; } return size; }
3.用fgetc计算一个文件有多少行,包装成函数(Linux操作系统以\n即使是最后一行也有一个结尾\n)
int line_file(FILE *fp)//计算文件行数 { int c = 0,line = 0;//行数初始化 while(1) { c = fgetc(fp); if(c<0) { break; }else if(c==10) { line ; } } return line; }
测试代码
#include <stdio.h> #include <stdlib.h> void copy_file(FILE *fp, FILE *fq); int size_file(FILE *fp); int line_file(FILE *fp); int main(int argc, const char *argv[]) { FILE *fp = fopen(argv[1], "r"); FILE *fq = fopen(argv[2], "w"); if(fp == NULL || fq == NULL) { perror("foen"); return -1; } copy_file(fp, fq);//文件argv[1]复制文件argv[2] // printf("%d\n",size_file(fp));//计算文件大小 // printf("%d\n",line_file(fp));//计算文件行数 fclose(fp); fclose(fq); return 0; }
结果:
ubuntu@ubuntu:day1$ ls 1.txt 2.txt a.out fgetc2.c ubuntu@ubuntu:day1$ cat 2.txt asf dgs chj dfj fkd 665 989 ab ubuntu@ubuntu:day1$ gcc fgetc2.c ubuntu@ubuntu:day1$ ./a.out 2.txt 3.txt ubuntu@ubuntu:day1$ ls 1.txt 2.txt 3.txt a.out fgetc2.c ubuntu@ubuntu:day1$ cat 3.txt asf dgs chj dfj fkd 665 989 ab
ubuntu@ubuntu:day1$ ubuntu@ubuntu:day1$ gcc fgetc2.c ubuntu@ubuntu:day1$ ./a.out 2.txt 3.txt 31 8 ubuntu@ubuntu:day1$ cat 2.txt asf dgs chj dfj fkd 665 989 ab ubuntu@ubuntu:day1$ cat -n 3.txt 1 asf 2 dgs 3 chj 4 dfj 5 fkd 6 665 7 989 8 ab ubuntu@ubuntu:day1$ ls -l 总用量 24 -rw-rw-r-- 1 ubuntu ubuntu 0 七月 27 18:32 1.txt -rw-rw-r-- 1 ubuntu ubuntu 31 七月 27 18:33 2.txt -rw-rw-r-- 1 ubuntu ubuntu 31 七月 27 18:34 3.txt -rwxrwxr-x 1 ubuntu ubuntu 8624 七月 27 18:36 a.out -rw-rw-r-- 1 ubuntu ubuntu 884 七月 27 18:36 fgetc2.c ubuntu@ubuntu:day1$
4.使用fgets和fputs复制一个文件,例如.c将内容复制到2.c
void copy_file(FILE *fp, FILE *fq) { char str[20]; while(fgets(str,sizeof(str),fp) != NULL) ///每次最多20-1个字符 { fputs(str, fq);///将取出的字符写入新文件 } }
测试代码:
#include <stdio.h> #include <stdlib.h> void copy_file(FILE *p, FILE *q); int main(int argc, const char *argv[]) { FILE *fp = fopen(argv[1], "r"); FILE *fq = fopen(argv[2], "w"); if(fp==NULL || fq==NULL) { perror("fopen"); return -1; } copy_file(fp, fq);//调用复制函数 fclose(fp); fclose(fq); return 0; }
结果:
ubuntu@ubuntu:hw$ cat 7.txt sfsdg 12345 hfh 54 d ubuntu@ubuntu:hw$ cat 8.txt ubuntu@ubuntu:hw$ gcc main.c ubuntu@ubuntu:hw$ ./a.out 7.txt 8.txt ubuntu@ubuntu:hw$ cat 7.txt sfsdg 12345 hfh 54 d ubuntu@ubuntu:hw$ cat 8.txt sfsdg 12345 hfh 54 d
5.使用fgetc计算文件的大小,并将其包装成函数
int size_file(FILE *fp) { char str[20] = "";////接收取出的字符串 int size = 0;///初始化大小 while(fgets(str,sizeof(str),fp) != NULL) { size = strlen(str);//字符串实际长度 } return size; }
测试代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> int size_file(FILE *fp); int main(int argc, const char *argv[]) { FILE *fp = fopen(argv[1], "r"); if(fp==NULL) { perror("fopen"); return -1; } printf("%d\n",size_file(fp));///调用函数计算文件大小 fclose(fp); return 0; }
测试结果:
ubuntu@ubuntu:hw$ gcc main.c ubuntu@ubuntu:hw$ ./a.out 7.txt 21 ubuntu@ubuntu:hw$ ls -l 7.txt -rw-rw-r-- 1 ubuntu ubuntu 21 七月 27 19:24 7.txt ubuntu@ubuntu:hw$ cat 7.txt sfsdg 12345 hfh 54 d
6.用fgetc计算一个文件有多少行,包装成函数(Linux操作系统以\n即使是最后一行也有一个结尾\n)
int line_file(FILE *fp) { char str[20]=""; int line = 0; while(fgets(str,sizeof(str),fp) != NULL) { if(strlen(str) < sizeof(str)-1) ///字符串长度超过19表示改行一次取不完,不是换行 { line ; } } return line; }
测试代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> int line_file(FILE *fp); int main(int argc, const char *argv[]) { FILE *fp = fopen(argv[1], "r"); if(fp==NULL) { perror("fopen"); return -1; } printf("%d\n",line_file(fp)); fclose(fp); return 0; }
测试结果:
#iubuntu@ubuntu:hw$ ./a.out 7.txt
10
ubuntu@ubuntu:hw$ cat -n 7.txt
1 sfsdg
2 auihyusahagygayhgsahygsuygxcyugy
3 ajuhgsad
4 shghgs
5 shghgs
6 dsghsghsfgsdfygdcsyhysughysudguhyghyuscfdguycgbyhusgcdhygsdcsdgbhsdbygssjhuyh
7 sjhuyh
8 syw
9 inh
10 mnj
ubuntu@ubuntu:hw$ ./a.out 8.txt
5
ubuntu@ubuntu:hw$ cat -n 8.txt
1 sfsdg
2 12345
3 hfh
4 54
5 d
ubuntu@ubuntu:hw$