1、fputs和fgetc相关函数解释
1]、字符的输出
#include<stdio.h>
int getc(FILE *fp)
int fgetc(FILE *fp)
int getchar(vaid)
3个函数若成功返回读入的字符值,若出错或则到末尾返回EOF,EOF为常量是-1
2]、字符的输入
#include<stdio.h>
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<stdio.h>
#include<stdlib.h>
#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个文件如下
运行代码之后
test.txt文件如下