当前位置:网站首页>Use of file i/o in C
Use of file i/o in C
2022-07-27 05:01:00 【Learn how to pass from the bottom to the middle】
C Documents in I/O There are several functions that we need to know , Yes open function ,read function ,write function , as well as close function .
open Use of functions
open:
Open a file
NAME
open, openat, creat - open and possibly create a file
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
pathname: Pathname / file name ( Not in the current directory , Path required )// Is all paths ?
flags: Document mark , Indicates the read-write permission of the file
O_RDONLY read-only
O_WRONLY Just write
O_RDWR Can read but write
Return value : Successfully returned a new file descriptor
Failure to return -1, meanwhile error Set up
close Use of functions
close:
Close a file
NAME
close - close a file descriptor
SYNOPSIS
#include <unistd.h>
int close(int fd);
fd: File descriptor , The file we opened
read Use of functions
read:
Read from file
NAME
read - read from a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
fd: File descriptor , The file we opened
buf: Where we will store the content we read
count: How many bytes to read
Return value : Successfully returned the number of bytes read ( return 0 Hour means that the document has been read )
Failure to return -1, meanwhile error Set up
write Use of functions
write:
Write content to file
NAME
write - write to a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
fd: File descriptor , The file we opened
buf: What we want to write
count: How many bytes to write
Return value : Successfully returned the number of bytes written ( return 0, The representative didn't write it in )
Failure to return -1, meanwhile error Set up
When we use system files , It is necessary to pay attention to the use of the specified header files , Use the functions in the header file to complete the functions we want .
practice :
Through the call of system functions, the things in a function , Write it into another function .
#include <sys/types.h>
#include <sys/stat.h>
#include<stdio.h>
#include <fcntl.h>
int main()
{
int fd = open("nice.c",O_RDWR);
int fd2=open("cp1.c",O_RDWR);
char buf[5000]={
0};// There is actually a flaw here , This is a waste of space .
while(1)
{
int read1;
read1= read(fd,buf,1);
write(fd2,buf,1);
if(read1==0)
{
close(fd2);
close(fd);
break;
}
}
return 0;
}
Last , Let's summarize , Is actually through the system I/O To operate files is actually to operate fd File operators .
边栏推荐
- Sunset red warm tone tinting filter LUTS preset sunset LUTS 1
- 报错:cannot read poperties of undefined(reading ‘then‘)
- HCIA static routing comprehensive experiment
- .htaccess learning
- [C language] dynamic memory management
- Basic configuration of static routing to achieve network wide accessibility
- CDH集群集成外部Flink(改进版-与时俱进)
- 日落红暖色调调色滤镜luts预设Sunset LUTs 1
- js小技巧
- 2019强网杯upload
猜你喜欢

Final Cut Pro Chinese tutorial (1) basic understanding of Final Cut Pro

strlen和sizeof的区别

.htaccess learning

Network protocol details: IP

MySQL下载安装 & 完美卸载

TCP's three handshakes and four waves

Affine transformation module and conditional batch Standardization (CBN) of detailed text generated images
![[error reporting]: cannot read properties of undefined (reading 'prototype')](/img/e4/528480610b9eed6028d7b3b87571e2.png)
[error reporting]: cannot read properties of undefined (reading 'prototype')

日落红暖色调调色滤镜luts预设Sunset LUTs 1
![[search] connectivity model of DFS + search order](/img/67/40444e53621371b1c8b47f22ebc956.png)
[search] connectivity model of DFS + search order
随机推荐
使用Photoshop出现提示“脚本错误-50出现一般Photoshop错误“
文件对话框
小程序项目如何创建
缓存读写策略:CacheAside、Read/WriteThrough及WriteBack策略
Three paradigms, constraints, some keyword differences,
【报错】Cannot read property ‘parseComponent‘ of undefined
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
Web框架介绍
Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
CEPH operation
How to import PS style? Photoshop style import tutorial
背包问题dp
多态的详讲
Photoshop裁剪工具隐藏技巧
Digital integrated circuit: MOS tube device chapter (II)
Customize the viewport height, and use scrolling for extra parts
Structural mode - bridging mode
"Photoshop2021 introductory tutorial" flatten "perspective images
Basic configuration of static routing to achieve network wide accessibility
QT 菜单栏、工具栏和状态栏