当前位置:网站首页>作业7.27 IO进程
作业7.27 IO进程
2022-07-28 01:07:00 【不知名大学生M】
1. 用fgetc和fputc实现,拷贝一个文件,例如将1.c的内容拷贝给2.c
代码
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen(argv[1],"r");
if(NULL==fp)
{
perror("fopen");
return -1;
}
FILE *fp2=fopen(argv[2],"w");
if(NULL==fp2)
{
perror("fopen");
return -1;
}
char a;
while((a=fgetc(fp))!=-1)
{
fputc(a,fp2);
}
fclose(fp);
fclose(fp2);
return 0;
}
运行结果
将helloworld.c的内容拷贝给test.c


2. 用fgetc计算一个文件的大小,要求封装成函数
函数代码
int size(const char *str)
{
FILE *fp=fopen(str,"r");
if(NULL==fp)
{
perror("fopen");
return -1;
}
int count=0;
while(fgetc(fp)!=-1)
{
count++;
}
fclose(fp);
return count;
}
3. 用fgetc计算一个文件有几行,要求封装成函数(linux操作系统以\n结尾,就算是最后一行也有一个’\n’)
函数代码
int line(const char *str)
{
FILE *fp=fopen(str,"r");
if(NULL==fp)
{
perror("fopen");
return -1;
}
int count=0;
char a;
while((a=fgetc(fp))!=-1)
{
if(a=='\n')
{
count++;
}
}
fclose(fp);
return count;
}
主函数代码
int main(int argc, const char *argv[])
{
int a=size(argv[1]);
int b=line(argv[1]);
printf("文件大小:%d\n文件行数:%d\n",a,b);
return 0;
}
运行结果
计算helloworld.c文件的大小和行数

4. 用fgets和fputs实现,拷贝一个文件,例如将1.c的内容拷贝给2.c
代码
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen(argv[1],"r");
if(NULL==fp)
{
perror("fopen");
return -1;
}
FILE *fp2=fopen(argv[2],"w");
if(NULL==fp2)
{
perror("fopen");
return -1;
}
char a[100];
while(fgets(a,sizeof(a),fp)!=NULL)
{
fputs(a,fp2);
}
fclose(fp);
fclose(fp2);
return 0;
}
运行结果
将helloworld.c的内容拷贝给test.c


5. 用fgets计算一个文件的大小,要求封装成函数
函数代码
int size(const char *str)
{
FILE *fp=fopen(str,"r");
if(NULL==fp)
{
perror("fopen");
return -1;
}
int count=0;
char a[100];
while(fgets(a,sizeof(a),fp)!=NULL)
{
count+=strlen(a);
}
fclose(fp);
return count;
}
6. 用fgets计算一个文件有几行,要求封装成函数(linux操作系统以\n结尾,就算是最后一行也有一个’\n’)
函数代码
int line(const char *str)
{
FILE *fp=fopen(str,"r");
if(NULL==fp)
{
perror("fopen");
return -1;
}
int count=0;
char a[100];
while((fgets(a,sizeof(a),fp))!=NULL)
{
if(a[strlen(a)-1]=='\n')
{
count++;
}
}
fclose(fp);
return count;
}
主函数代码
int main(int argc, const char *argv[])
{
int a=size(argv[1]);
int b=line(argv[1]);
printf("文件大小:%d\n文件行数:%d\n",a,b);
return 0;
}
运行结果
计算helloworld.c文件的大小和行数

边栏推荐
- Mysql Explain 详解(荣耀典藏版)
- Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
- 这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】
- Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements
- Under the new retail format, retail e-commerce RPA helps reshape growth
- Alipay applet authorization / obtaining user information
- 剑指offer专项突击版第12天
- Flask1.1.4 werkzeug1.0.1 source code analysis: Blueprint
- Class notes (5) (1) - 593. Binary search
- "Risking your life to upload" proe/creo product structure design - seam and buckle
猜你喜欢

Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements

新零售业态下,零售电商RPA助力重塑增长

使用BigDecimal类型应该避免哪些问题?(荣耀典藏版)

LeetCode 热题 HOT 100 -> 3. 无重复字符的最长子串

CeresDAO:Ventures DAO的“新代言”
![This operation may not be worth money, but it is worth learning | [batch cutting of pictures]](/img/e8/a34e471b0089f8085b140c74b5c01f.jpg)
This operation may not be worth money, but it is worth learning | [batch cutting of pictures]

Flex layout - fixed positioning + flow layout - main axis alignment - side axis alignment - expansion ratio

Product interpretation - Design and distributed expansion of metersphere UI test module

Unity saves pictures to albums and rights management

APP如何上架App Store?
随机推荐
[database data recovery] data recovery case of insufficient disk space of SQL Server database
Data output - dynamic drawing
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
Digital empowerment and innovation in the future: hese eredi appears at the 5th Digital China Construction Summit
视频常用分辨率
一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由
【Star项目】小帽飞机大战(五)
SkyWalking分布式系统应用程序性能监控工具-中
Synchronized details
【HCIP】路由策略、策略路由
Aike AI frontier promotion (7.14)
获取两个集合相差数据
四种常见的 POST 提交数据方式
【ROS进阶篇】第九讲 基于Rviz和Arbotix控制的机器人模型运动
MySQL高可用和主从同步
支付宝小程序授权/获取用户信息
Smart contract security -- selfdestroy attack
The level "trap" of test / development programmers is not a measure of one-dimensional ability
清除浮动的原因和六种方法(解决浮动飞起影响父元素和全局的问题)
How MySQL uses indexes (glory Collection Edition)
