【Linux】文件操作函数(系统调用函数)
- 重点在于学习——思路与方法
- 举一反三

一、文件描述符
- 系统分配给文件的数字编号
二、函数学习

P.S.Man命令使用方法
manual 前三个章节 命令;系统调用函数;库函数
man read //read命令
man 2 read //系统调用函数read
第2类 系统调用文件编程类
2.1 打开文件
2.1.1 函数名
open
2.1.2 函数原形
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
2.1.3 函数功能
open and possibly create a file or device
2.1.4 所属头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2.1.5 返回值
return the new file descriptor,
or -1 if an error occurred .
2.1.6 参数说明
pathname //文件路径名
flags //access mode:O_RDONLY,O_WRONLY,or O_RDWR.more flags to search——man COMMAND
-O_APPEND:以追加方式打开文件
-O_CREAT:当打开的文件不存在的时候,创建该文件
mode: // if flag = O_CREAT , mode 用于设置新创建文件的访问权限
P.S.参数可位或
fd = open ("/home/test.c".O_RDWR|O_CREAT,0755);
2.2 创建文件
2.2.1 函数名
creat
2.2.2 函数原形
int creat(const char *pathname, mode_t mode);
2.2.3 函数功能
creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC.
创建一个文件,并以只写的方式打开该文件
2.2.4 所属头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2.2.5 返回值
success:file descriptior
fali: -1
2.2.6 参数说明
pathname //文件路径名
mode //新建文件的读写权限设置
2.3 关闭文件
2.3.1 函数名
close
2.3.2 函数原形
int close(int fd);
2.3.3 函数功能
关闭一个打开的文件
2.3.4 所属头文件
#include <unistd.h>
2.3.5 返回值
success:0
error :-1
2.3.6 参数说明
fd :待关闭文件的file descriptor
2.4 读文件
2.4.1 函数名
read
2.4.2 函数原形
//-man 2 read
ssize_t read(int fd, void *buf, size_t count);
2.4.3 函数功能
从一个打开的文件中读取数据
2.4.4 所属头文件
#include <unistd.h>
2.4.5 返回值
success:返回读取的字节数
error:-1
2.4.6 参数说明
fd :待读取数据的文件的file descriptor
count:希望读取的字节数
buf: 读取来的数据存到buf指向的缓冲区
2.5 写文件
2.5.1 函数名
write
2.5.2 函数原形
//-man 2 read
ssize_t write(int fd, const void *buf, size_t count);
2.5.3 函数功能
向一个打开的文件写入数据
2.5.4 所属头文件
#include <unistd.h>
2.5.5 返回值
success:写入到文件里的字节数
error:-1
2.5.6 参数说明
fd :待写入数据的文件的file descriptor
count:希望写入的字节数
buf: 要写入数据的存放位置
2.6 重定位文件读写位置
2.6.1 函数名
lseek
2.6.2 函数原形
off_t lseek(int fd, off_t offset, int whence);
2.6.3 函数功能
reposition read/write file offset
重新定位文件的读写位置(指针)
2.6.4 所属头文件
#include <sys/types.h>
#include <unistd.h>
2.6.5 返回值
success:返回移动后的文件指针距离文件开头的字节数位置
error:-1
2.6.6 参数说明
fd :待写入数据的文件的file descriptor
The lseek() function repositions the offset of the open file associated with the file descriptor fd to the argument offset according to the directive whence as follows:
//无论指针现在在何处,whence设置offset开始移动的起始位置
SEEK_SET
The offset is set to offset bytes.
SEEK_CUR
The offset is set to its current location plus offset bytes.
SEEK_END
The offset is set to the size of the file plus offset bytes.
2.7 复制文件描述符
2.7.1 函数名
dup
2.7.2 函数原形
int dup(int oldfd)
2.7.3 函数功能
复制一个文件描述符
2.7.4 所属头文件
#include <unistd.h>
2.7.5 返回值
success:返回新的文件描述符
error: -1
2.7.6 参数说明
oldfd:待复制的旧的文件描述符
三、综合实例 ——手动实现cp命令功能
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void main(int argc,char **argv)
{
int fd_source;
int fd_destination;
char buf[512];
int count = 0;
/*1.打开源文件*/
fd_source = open(argv[1],O_RDONLY);
/*2.打开目标文件*/
fd_destination = open(argv[2],O_RDWR|O_CREAT,0666);
/*3.读取源文件,循环分块读写*/
while(count = read (fd_source,buf,512)>0)
/*4.将数据写入目标文件*/
{
write(fd_destination,buf,count);
}
/*5.关闭文件*/
close(fd_source);
close(fd_destination);
}
【Linux】文件操作函数(系统调用函数)的更多相关文章
- LInux文件基础知识和文件目录操作(系统调用函数方式)
1.进程是处于活动状态的程序,某个用户通过操作系统运行程序所产生的进程代表着该用户的行为.如果用户不具备访问某个目录和文件的权限,那么该用户的进程也不能访问. 2.Linux系统中文件安全机制是通过给 ...
- Linux C高级编程——文件操作之系统调用
Linux C高级编程文件操作之系统调用 宗旨:技术的学习是有限的,分享的精神是无限的. 库函数是一些完毕特定功能的函数.一般由某个标准组织制作公布,并形成一定的标准.使用库函数编 ...
- PYDay6- 内置函数、验证码、文件操作、发送邮件函数
1.内置函数 1.1Python的内置函数 abs() dict() help() min() setattr() all() dir() hex() next() slice() any() div ...
- Linux文件操作学习总结【转载】
本文转载自: http://blog.csdn.net/xiaoweibeibei/article/details/6556951 文件类型:普通文件(文本文件,二进制文件).目录文件.链接文件.设备 ...
- C/C++以及Linux文件操作备忘录
目录 C文件操作 文件开关 文件读写 C++文件操作 Linux文件操作 打开 C文件操作 #include<stdio.h> stdin, stdout, stderr 文件开关 /* ...
- Linux 文件操作接口
目录 Linux 文件操作接口 C语言文件操作接口 C语言文件描述 fopen() r模式打开文件 w模式打开文件 a模式打开文件 其他模式类似 fclose() fwrite() fread() 系 ...
- Linux C 文件操作,系统调用 -- open()、read() 和 标准I/O库 -- fopen()、fread()
函数汇总: open().write().read().close() fopen().fwrite().fread().fclose() 一.什么是文件 在讲述文件操作之前,我们首先要知道什么是文件 ...
- Linux 文件操作——系统调用和标准I/O库
一.什么是文件 在讲述文件操作之前,我们首先要知道什么是文件.看到这个问题你可能会感觉到可笑,因为对于用过计算机的人来说,文件是最简单不过的概念了,例如一个文本是一个文件,一个work文档是一个文件等 ...
- Windows文件操作的API函数[转载]
在VC中,大多数情况对文件的操作都使用系统提供的 API 函数,但有的函数我们不是很熟悉,以下提供一些文件操作 API 函数介绍: 一般文件操作 API CreateFile 打开文件 要对文件进行读 ...
随机推荐
- VSLAM技术框架详述
最早的SLAM雏形是在军事(核潜艇的海底定位)上的应用,主要传感器是军用雷达.SLAM技术发展到如今已经几十年,目前以激光雷达作为主传感器的SLAM技术比较稳定.可靠,仍然是主流的技术方案.但随着最近 ...
- vue is detected
Vue.js is detected on this page. Devtools inspection is not available because it's in production mod ...
- Linux环境查看系统参数
一.查看CPU信息 lscpu cat /proc/cpuinfo 二.查看CPU位数 getconf LONG_BIT 三.查看MEM信息 free free -m cat /proc/me ...
- windows cmd下创建虚拟环境virtualenv
一:虚拟环境virtualenv 如果在一台电脑上, 想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新版本会覆盖以前的版本, 其它的项目就无 ...
- Android 应用资源及R文件的位置
1.介绍 (1)常识 (2)在res目录下新建资源文件(例如数字资源) app--->res,选择res,右击new--->value resource file 2.字符资源(strin ...
- Nginx负载均衡详解
upstream mysvr { server 192.168.10.121:3333; server 192.168.10.122:3333; } server { .... location ...
- 下载 生成 requirements
生成你项目的所有 组件,模块 pip3 freeze > requirements.txt 下载requirements.txt 里的所有 模块 pip3 install -r requirem ...
- docker 镜像编译
docker为我们提供了,包含源码的镜像, 可以要从docker hub上下载镜像来编译docker源码. 1. docker pull docker-dev:v1.2.0,其他版本就到docker ...
- drf序列化器的实例
应用目录结构: views.py from django.shortcuts import render # Create your views here. from django.views imp ...
- TCP/IP协议分为哪四层,具体作用是什么。
TCP/IP通讯协议采用了4层的层级结构,每一层都呼叫它的下一层所提供的网络来完成自己的需求.这4层分别为: 应用层:应用程序间沟通的层,如简单电子邮件传输(SMTP).文件传输协议(FTP).网络远 ...