fgetc、fputc、fputs、fgets的使用

本文展示了使用fgetc和fputc函数复制文件,计算文件大小以及行数的方法。通过示例代码,演示了如何实现文件内容的拷贝,并提供了计算文件大小和行数的函数。测试结果显示,代码能够正确执行文件拷贝和统计操作。
摘要由CSDN通过智能技术生成

1.使用fgetc和fputc拷贝一个文件,例如将1.c的内容拷贝到2.c

void copy_file(FILE *fp, FILE *fq) //fp被拷贝文件地址,fq要拷贝的文件地址
{
	int c = 0;//接收fgetc函数的返回值
	while(1)
	{
		c = fgetc(fp);
		if(c<0)//文件结束跳出循环
		{
			break;	
		}
		fputc(c, fq);//将字符c写入到fq
	}
}

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拷贝一个文件,例如将1.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$ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傾语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值