当前位置:网站首页>Job 7.27 IO process
Job 7.27 IO process
2022-07-28 02:46:00 【Unknown college student M】
1. use fgetc and fputc Realization , Copy a file , For example 1.c Copy your content to 2.c
Code
#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;
}
Running results
take helloworld.c Copy your content to test.c


2. use fgetc Calculate the size of a file , Require encapsulation into functions
function code
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. use fgetc Calculate how many lines a file has , Require encapsulation into functions (linux Operating system to \n ending , Even the last line has one ’\n’)
function code
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;
}
Main function code
int main(int argc, const char *argv[])
{
int a=size(argv[1]);
int b=line(argv[1]);
printf(" file size :%d\n File lines :%d\n",a,b);
return 0;
}
Running results
Calculation helloworld.c File size and number of lines

4. use fgets and fputs Realization , Copy a file , For example 1.c Copy your content to 2.c
Code
#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;
}
Running results
take helloworld.c Copy your content to test.c


5. use fgets Calculate the size of a file , Require encapsulation into functions
function code
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. use fgets Calculate how many lines a file has , Require encapsulation into functions (linux Operating system to \n ending , Even the last line has one ’\n’)
function code
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;
}
Main function code
int main(int argc, const char *argv[])
{
int a=size(argv[1]);
int b=line(argv[1]);
printf(" file size :%d\n File lines :%d\n",a,b);
return 0;
}
Running results
Calculation helloworld.c File size and number of lines

边栏推荐
- How do you use the jar package sent by others (how to use the jar package sent by others)
- Eigenvalues and eigenvectors
- [hcip] BGP Foundation
- Product axure9 English version, using repeater repeater repeater to realize multi-choice and single choice
- [image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code
- New infrastructure helps the transformation and development of intelligent road transportation
- Canonical Address
- How MySQL uses indexes (glory Collection Edition)
- 【自我成长网站收集】
- Interviewer: what is the factory method mode?
猜你喜欢
![[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code](/img/a7/8f0d929457d285adc8020c7f5d33ea.png)
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code

MySQL's way to solve deadlock - lock analysis of common SQL statements

Wechat campus bathroom reservation applet graduation design finished product (1) development outline

Notes for the fourth time of first knowing C language

Typescript (zero) -- introduction, environment construction, first instance

JS中的reduce()函数介绍

JVM tuning -xms -xmx -xmn -xss

Lock mechanism in MySQL database InnoDB storage engine (glory Collection Edition)

基于stm32的恒功率无线充电

Read Plato & nbsp; Eplato of farm and the reasons for its high premium
随机推荐
Common SQL statement query
【自我成长网站收集】
【OpenGL】GLES20.glClear
Feign calls get and post records
Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
功能测试和非功能测试区别简析,上海好口碑软件测试公司推荐
[software testing] - unittest framework for automated testing
Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
Sqlserver problem solving: replication components are not installed on this server. Please run SQL Server Setup again and select the option to install replication components
Which users are suitable for applying for rapidssl certificate
PS simple to use
【软件测试】—— 自动化测试之unittest框架
分层图解决的一些最短路问题
Leetcode judge whether palindrome number
One month's experience of joining Huawei OD
【TA-霜狼_may-《百人计划》】图形3.7 移动端TP(D)R架构
A 64 bit 8-stage pipelined adder based on FPGA
In practical work, how do I use postman for interface testing?
Usage of delegate
Learn this trick and never be afraid to let the code collapse by mistake
