Linux字符串拷贝函数,linux c之用fputc和fgetc复制文件并且打印在终端

1、fputs和fgetc相关函数解释

1、字符的输出

#include

int getc(FILE *fp)

int fgetc(FILE *fp)

int getchar(vaid)

3个函数若成功返回读入的字符值,若出错或则到末尾返回EOF,EOF为常量是-1

2、字符的输入

#include

int putc(int c, FILE *fp)

int fputc(int c, FILE *fp)

int putchar(int c)

3个函数返回,若成功则为c,若出错返回EOF

2、代码复制文件内容并且终端打印的实现

我需要把hello.txt里面的内容复制到test.txt文件去,并把内容打印出来

#include

#include

#define PATH1 "/home/chenyu/Desktop/linux/hello.txt"

#define PATH2 "/home/chenyu/Desktop/linux/test.txt"

int main(void)

{

FILE *pf1, *pf2;

int k;

if((pf1 = fopen(PATH1,"r")) == NULL){

puts("Open file error!\n");

exit(EXIT_FAILURE);}

if((pf2 = fopen(PATH2,"w")) == NULL) {

puts("open pf2 error\n");

exit(EXIT_FAILURE);

}

printf("open 2 txt success\n");

while((k = fgetc(pf1)) != EOF) {

if(fputc(k, pf2) == EOF ) {

printf("write data to test.txt fail");

}

if(fputc(k,stdout) == EOF) {

printf("write screen test.txt fail");

}

}

printf("EOF is %d", EOF);

if (fclose(pf1) != 0)

puts("Close file error!");

if (fclose(pf2) != 0)

puts("Close file error");

return 0;

}

3、结果展示

没有运行代码之前2个文件如下

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

运行代码之后

0818b9ca8b590ca3270a3433284dd417.png

test.txt文件如下

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值