当前位置:网站首页>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 .
边栏推荐
- Knapsack problem DP
- Digital integrated circuit: MOS tube device chapter (I)
- HCIA static routing basic simulation experiment
- How to import PS style? Photoshop style import tutorial
- Basic configuration of static routing to achieve network wide accessibility
- ps怎么导入lut预设?Photoshop导入lut调色预设教程
- 【Acwing】第61场周赛 题解
- 消防安全培训资料汇总
- SVN使用详解
- Error: cannot read properties of undefined (reading 'then')
猜你喜欢

"Photoshop2021 tutorial" align and distribute to make dot patterns

kali系统arp介绍(断网嗅探密码抓包)

MySQL storage engine and its differences

What if Photoshop prompts that the temporary storage disk is full? How to solve the problem that PS temporary storage disk is full?

Basic configuration of static routing to achieve network wide accessibility

2019 top tennis cup upload

.htaccess学习

HCIA dynamic routing OSPF experiment

多态的详讲

使用Photoshop出现提示“脚本错误-50出现一般Photoshop错误“
随机推荐
文件对话框
Be diligent in talking about what sidelines you can do now
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
.htaccess学习
日落红暖色调调色滤镜luts预设Sunset LUTs 1
Detailed explanation of mvcc and its principle
C language address book management system (linked list, segmented storage of mobile phone numbers, TXT file access, complete source code)
TCP's three handshakes and four waves
事件总结-常用总结
Digital integrated circuit: CMOS inverter (I) static characteristics
【搜索】DFS之连通性模型 + 搜索顺序
节流函数的demo——正则表达式匹配
Read write separation and master-slave synchronization
Web框架介绍
Three paradigms, constraints, some keyword differences,
Affine transformation module and conditional batch Standardization (CBN) of detailed text generated images
网络协议详解:IP
Photoshop裁剪工具隐藏技巧
[error reporting]: cannot read properties of undefined (reading 'prototype')
背包问题dp