IO学习系列之使用fgetc和fputc复制文件内容

  • fgetc函数:
  • 功能:在文件中读取一个字符;
  • 具体内容如下:
c 复制代码
#include <stdio.h>   //头文件

int fgetc(FILE *stream);
/*
	参数:	
			stream 文件指针
			
	返回值:
	
    		成功 	返回读取到的字符
    		
    		失败OR文件结束 	返回EOF 
*/
  • fputc函数:
  • 功能:在文件中写入一个字符;
  • 具体内容如下:
c 复制代码
#include <stdio.h>   //头文件

int fputc(int c, FILE *stream);
/*
	参数:
	
    	c     	要操作的字符
    	
    	stream 	文件指针
    	
	返回值:
	
    	成功 	返回写入的字符的ASCII码
    	
    	失败 	返回EOF 
 */
  • perror函数:
  • 功能:错误码被重置后,直接打印错误信息;
  • 具体内容如下:
c 复制代码
#include <stdio.h>	//头文件

void perror(const char *s);
/*
	参数:	
	
		s 	用户附加的信息 
		
    	最终打印的结果为:
    	
    		附加的信息和错误码对应的错误信息以及换行符
    	
	返回值:	无
*/
  • 示例代码:
c 复制代码
//使用 fgetc 和 fputc 函数实现文件拷贝的功能

#include <stdio.h>

int main(int argc, const char *argv[])
{
    
    if(3 != argc)
    {

        printf("Usage : %s src_file dest_file\n",argv[0]);

        return -1;
    }

    FILE *fp1 = fopen(argv[1],"r");

    if(NULL == fp1)
    {
        perror("fopen error");
        return -1;
    }

    FILE *fp2 = fopen(argv[2],"w");

    if(NULL == fp1)
    {
        perror("fopen error");
        return -1;
    }

    int ret = 0;

    while(EOF != (ret = fgetc(fp1)))\
    {

        fputc(ret,fp2);
    }
    return 0;
}
相关推荐
ん贤11 小时前
手敲Linux命令
linux·运维·服务器
泽020211 小时前
OJBalancer ----- 基于负载均衡仿leetcode的刷题界面
linux·leetcode·负载均衡
会编程的土豆11 小时前
常用算法里的细节
数据结构·c++·算法·图论
skilllite作者11 小时前
为什么我认为 Hermes 需要说明 self-evolution 的设计来源
人工智能·算法·rust·openclaw·agentskills
花间相见11 小时前
【Linux进阶01】—— tmux原理与实战教程
linux·运维·服务器
Bert.Cai11 小时前
Linux groupadd命令详解
linux·运维
tankeven11 小时前
HJ179 小苯的IDE括号问题(easy)
c++·算法
路溪非溪11 小时前
抓取手机的蓝牙HCI日志并分析
linux·arm开发·驱动开发·智能手机
有谁看见我的剑了?11 小时前
新服务器上线优化调整
linux·运维·服务器
成为你的宁宁11 小时前
【apt update突然报错Temporary failure resolving ‘cn.archive.ubuntu.com‘】
linux·运维·ubuntu