当前位置:网站首页>By using the fgets () the number of rows in the statistics file and use the fgets ()/fputs () copy files
By using the fgets () the number of rows in the statistics file and use the fgets ()/fputs () copy files
2022-08-08 07:05:00 【night cat xu】
要求:
1.使用fgetsline number of the statistics file
2.使用fgets/fputs拷贝文件
思路:
1.在./a.outThen enter a file to count the line number of the file
2.在./a.outThen enter two files to copy the first file and copy it to the second file.Create a second file and make a copy when the second file does not exist.
源代码如下:
main.c
#include <head.h>
#include "homework.h"
int main(int argc, const char *argv[])
{
FILE *fp, *fp1;
int a = argc;
if (a == 2)
{
if ((fp = fopen(argv[1], "r")) == NULL)
PRINT_ERR("打开文件失败");
line_file(a, fp);
}
else if (a == 3)
{
if ((fp = fopen(argv[1], "r")) == NULL)
PRINT_ERR("打开文件失败");
if ((fp1 = fopen(argv[2], "a")) == NULL)
PRINT_ERR("打开文件失败");
copy_file(a, fp, fp1);
}
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define PRINT_ERR(msg) \ do{
\ perror(msg); \ return -1; \ }while(0)
#endif
work.c
#include <head.h>
int copy_file(int a, FILE *fp, FILE *fp1)
{
char buffer[10];
if (a != 3)
{
printf("input error,try again\n");
printf("usage:./a.out filename filename\n");
return -1;
}
while ((fgets(buffer, sizeof(buffer), fp)) != NULL)
{
fputs(buffer, fp1);
}
fclose(fp);
fclose(fp1);
return 0;
}
int line_file(int a, FILE *fp)
{
int num = 0;
char buffer[10];
if (a != 2)
{
printf("input error,try again\n");
printf("usage:./a.out filename\n");
return -1;
}
while ((fgets(buffer, sizeof(buffer), fp)) != NULL)
{
if (buffer[strlen(buffer) - 1] == '\n')
{
num++;
}
}
printf("该文件有%d行\n", num);
fclose(fp);
return 0;
}
work.h
#ifndef __HOMEWORK_H__
#define __HOMEWORK_H__
int copy_file(int a, FILE *fp, FILE *fp1);
int line_file(int a, FILE *fp);
#endif
边栏推荐
猜你喜欢
深度神经网络主要模型,深度神经网络预测模型
Meta-Learning and in-context Learning
网络配置——静态路由内网以及外网连接
Paramenter-Efficient Transfer Learning for NLP
NVIDIA CUDA Highly Parallel Processor Programming (6): Parallel Mode: Convolution
NVIDIA CUDA Highly Parallel Processor Programming (8): Parallel Mode: Histogram Calculation
RNN神经网络适用于什么,RNN神经网络基本原理
OSPF动态配置网络环境
[Unity] GPU动画实现(四)——生成动画数据
顺序循环队列的创建和基本应用
随机推荐
Unity3D物体上下左右旋转(不受物体自身坐标轴影响)
Unity 物体颜色渐变效果(判断逻辑实现)
C语言实现链表的增删查改以及OJ题讲解
acwing 第63场周赛【2022.08.06】
虚拟机克隆 快照 迁移 删除
最详细的Vivado安装教程
HDU 6029 个人分析
Next主题美化
关系抽取论文阅读笔记
C# Unicode (Universal Code) text conversion
线程P01——进程 并发 并行 线程的使用
Unity_组件自动绑定
rhcsa第二天
C# Unicode(万国码)文字转换
通过使用fgets()统计文件的行号和使用fgets()/fputs()拷贝文件
深度神经网络主要模型,深度神经网络预测模型
ACM latex
P17 五子棋的实现4 悔棋功能
firefly rk3399 硬件解码,多通道解码
leetcode第84场双周赛