04文件与IO
文件系统调用:
open、close、create、read、write
open:
int open(const char* path, int flags)
path:要打开的文件的路径
flags:打开的模式
执行结果:成功返回文件描述符,失败返回-1。
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h> //普通的宏定义写法,多条语句用逗号表达式。
//#define ERR_EXIT(m) (perror(m), exit(EXIT_FAILURE)) //这种宏定义的写法更为专业,工作中都是用这种
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while() int main(void)
{
int fd;
fd = open("test.txt", O_RDONLY);
/*
if (fd == -1)
{
fprintf(stderr, "open error with errno=%d %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
*/
/*
if (fd == -1)
{
perror("open error");
exit(EXIT_FAILURE);
}
*/
if (fd == -)
ERR_EXIT("open error"); printf("open succ\n");
return ;
}
int open(const char *path, int flags,mode_t mode);
打开成功,返回文件描述符;
打开失败,返回-1
|
打开方式 |
描述 |
|
O_RDONLY |
打开一个供读取的文件 |
|
O_WRONLY |
打开一个供写入的文件 |
|
O_RDWR |
打开一个可供读写的文件 |
|
O_APPEND |
写入的所有数据将被追加到文件的末尾 |
|
O_CREAT |
打开文件,如果文件不存在则建立文件 |
|
O_EXCL |
如果已经置O_CREAT且文件存在,则强制open()失败 |
|
O_TRUNC |
在open()时,将文件的内容清空 |
其中注意打开的文件的权限不是mode而是mode&^umask。
自己可以用umask命令查看当前的umask值,在程序中也可以用umask()来指定umask的值,例如:umask(0)。
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h> #define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0) int main(void)
{
umask(0);
int fd;
fd = open("test.txt", O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd == -1)
ERR_EXIT("open error"); printf("open succ\n");
return 0;
}
权限可以用数字的形式也可以用下面这种,不过数字更直观一点。
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h> #define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0) int main(void)
{
umask(0); //在程序中自己指定umask值
int fd;
fd = open("test2.txt", O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd == -1)
ERR_EXIT("open error"); printf("open succ\n");
close(fd); //养成好习惯不用了就关闭掉
return 0;
}
如打开一个新文件:
#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)
#include <sys/stat.h> 访问到,同样可以通过|运算来对访问权限进行组合
#define MODE755 (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
04文件与IO的更多相关文章
- 【Go】使用压缩文件优化io (二)
原文链接: https://blog.thinkeridea.com/201907/go/compress_file_io_optimization2.html 上一篇文章<使用压缩文件优化io ...
- node源码详解(七) —— 文件异步io、线程池【互斥锁、条件变量、管道、事件对象】
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource7 本博客同步在https://cnodejs.o ...
- 文件和IO流
摘要:本文主要介绍了Java的文件处理以及常用的IO流操作. 文件操作 概念 File是数据源(保存数据的地方)的一种,可以表示一个文件,也可以表示一个文件目录. File类只能对文件和文件夹进行创建 ...
- Atitit.跨语言 文件夹与文件的io操作集合 草案
Atitit.跨语言 文件夹与文件的io操作集合 草案 1. Jdk原生的太难用了..1 2. PS: apache commons-io包,FileUtils有相关的方法,IOUtils一般是拷 ...
- PythonCookBook笔记——文件与IO
文件与IO 所有的程序都要处理输入与输出,涉及到文本.二进制文件.文件编码和对文件名.目录的操作. 读写文本数据 需要读写各种不同编码的文本数据,使用rt模式的open()函数. 该读写操作使用系统默 ...
- 提高生产力:文件和IO操作(ApacheCommonsIO-汉化分享)
复制.移动.删除.比较.监控.文件读写 等文件和IO操作是编程中比较常用的功能. 幸运的是,Apache Commons IO等开源组件已经帮我们实现了. 我们可以不用重复 ...
- linux系统编程之文件与io(四)
今天继续学习文件与io,主要是学习文件共享及文件.复制文件描述符,有点抽象,主要是概念上的理解,但是很重要,下面一一来分解: 文件共享: 回顾一下,在linux系统调用中,是通过文件描述符来访问文件的 ...
- linux系统编程之文件与io(二)
今天继续学习文件与io,话不多说,开始进入正题: 文件的read和write系统调用: 说明:函数中出现在size_t和ssize_t是针对系统定制的数据类型: 下面以一个实现文件简单拷贝的示 ...
- linux系统编程之文件与io(一)
经过了漫长的学习,C语言相关的的基础知识算是告一段落了,这也是尝试用写博客的形式来学习c语言,回过头来看,虽说可能写的内容有些比较简单,但是个人感觉是有史起来学习最踏实的一次,因为里面的每个实验都是自 ...
随机推荐
- html文字垂直居中
比如一个div 需设置height的高度和line-height一样. <div style="height:30px;line-height:30px;"> OK. ...
- [转载]架构指南 : Java1.7+Eclipse luna + Maven 3.2.5 +spring 4.1.4
1. 环境配置 a) Java 1.7 b) Eclipse luna c) Maven3.2.5 d) spring 4.1.4 2. ...
- HDU 5918 KMP/模拟
Sequence I Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 使用npoi.dll导出数据到excel
.net数据导出excel数据有多种方法,最常用的就是使用office组件,但随之而来的问题也很棘手,又要调权限又要确定是否安装office很是麻烦,最近一个项目中也有数据导出功能,随使用excel模 ...
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- spark优化之优化数据结构
概序: 要减少内存的消耗,除了使用高效的序列化类库以外,还有一个很重要的事情,就是优化数据结构.从而避免Java语法特性中所导致的额外内存的开销,比如基于指针的Java数据结构,以及包装类型. 有一个 ...
- timus 1136 Parliament(e)
Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...
- LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...
- Qt GUI@学习日志
day 1: Qt中类: 理解一个类最好还是从其类代码实现上看. 由此图可看出需要好好研究那几个重要的类:Qt/QEvent/QObject/QWidget/. QApplication: (比较复杂 ...
- Java——各种日期的获取(来自别人分享)
import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; i ...