2.1 打开文件和关闭文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 头文件
int open(const char *pathname, int flags); 打开一个文件
int close(int fildes); 关闭一个文件

1.打开文件
int open(const char *pathname, int flags);
//const char *pathname 是要打开的文件路径
//int flag 是文件打开的标志 。 标志有 主标志 和 副标志 。
// 主标志是互斥的。三选一
// O_RDONLY 只读方式打开
// O_RDWR  读写方式打开
// O_WRONLY 只写方式打开
// 副标志可以多选
// O_APPEND 读写文件从文件末尾处追加
// O_TRUNC 若文件存在并可写,则用清空的方式打开文件
// O_CREAT 若文件不存在,则创建该文件
// O_EXCL ??

// 如果用O_CREAT 方式创建不存在的文件, open则需要额外设置文件权限
int open(const char *pathname, int flags, mode_t mode);
//mode_t mode 用0755 或者其他权限写入即可 .
//创建文件的另一个函数,用法同open
int creat(const char* pathname, mode_t mode);

 

2.关闭文件
// 在使用完文件后,必须正常关闭文件!!

close(int fildes);

举个栗子:
/*

============================================================================
Name : hello.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>   //open
#include <unistd.h>  //close

int main(void)
{
  int fd;
  const char* file="./hello";  //当前文件夹下的hello文件

  fd=open(file, O_RDWR);     //先以读写方式打开
  if(fd<)              //如果该文件不存在,打开失败了
  {
    puts("no such file .");
    fd=open(file,O_RDWR|O_CREAT,);  //就加入创建副属性
    if(fd<)
    {
      puts("open file err !");
      return-;
    }
  }
  puts("open file success .");

  close(fd);                //最后记得关闭文件流
  puts("close file success .");
  return ;
}
 

linux文件操作篇 (二) 打开和关闭文件的更多相关文章

  1. linux文件IO操作篇 (二) 缓冲文件

    2. 缓冲文件操作 //规模较大 实时性低的文件 //当数据长度快要超过缓冲区的范围时,或者时间周期达到时,数据才被送往指定位置 //需要使用FILE * 作为文件标识符 //stdin 标准输入 / ...

  2. python04篇 文件操作(二)、集合

    一.文件操作(二) 1.1 利用with来打开文件 # with open ,python 会自动关闭文件 with open('a.txt', encoding='utf-8') as f: # f ...

  3. 文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write

    文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write close(关闭文件) 相关函数 ope ...

  4. ansible笔记(5):常用模块之文件操作(二)

    ansible笔记():常用模块之文件操作(二) 文件操作类模块 find模块 find模块可以帮助我们在远程主机中查找符合条件的文件,就像find命令一样. 此处我们介绍一些find模块的常用参数, ...

  5. Python打开和关闭文件

    Python打开和关闭文件: open(文件名,打开文件的模式[,寄存区的缓冲]): 文件名:字符串值 注:文件名带有后缀名 # 打开创建好的 test.txt 文件 f = open("t ...

  6. 重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作

    原文:重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作 [源码下载 ...

  7. linux文件操作篇 (四) 目录操作

    #include <sys/stat.h>#include <unistd.h>#include <dirent.h> //创建文件夹 路径 掩码 int mkdi ...

  8. PHP怎么打开或者关闭文件?

    什么是文件处理? 文件处理包括读取,关闭,重写等.掌握文件的处理需要读者理清思路,掌握好文件处理的关键步骤和常用函数,那么就可以运用自如了!感兴趣的请移步php文件处理专题. 比如,访问一个文件需要 ...

  9. C++文件操作(输入输出、格式控制、文件打开模式、测试流状态、二进制读写)

    1.向文件写数据 头文件#include <ofstream> ①Create an instance of ofstream(创建ofstream实例) ②Open the file w ...

随机推荐

  1. Oracle分析函数列表分享

    SUM        :该函数计算组中表达式的累积和 MIN        :在一个组中的数据窗口中查找表达式的最小值 MAX        :在一个组中的数据窗口中查找表达式的最大值 AVG     ...

  2. #单元测试#以karma+mocha+chai 为测试框架的Vue webpack项目(一)

    目标: 为已有的vue项目搭建 karma+mocha+chai 测试框架 编写组件测试脚本 测试运行通过 抽出共通 一.初始化项目 新建项目文件夹并克隆要测试的已有项目 webAdmin-web 转 ...

  3. pringMvc-使用原生api

    在springMvc有时候需要使用原生的api: @RequestMapping(value="/testApi") public String testApi(HttpServl ...

  4. March 12 2017 Week 11 Sunday

    I learned the value of hard work by working hard. 只有真的努力了,才会知道努力的价值. I know the value of hard work, ...

  5. NO.008-2018.02.13《折桂令·春情》元代:徐再思

    折桂令·春情_古诗文网   折桂令·春情 元代:徐再思 平生不会相思,才会相思,便害相思.生下来以后还不会相思,才刚刚懂了什么是相思,却深受着相思之苦. 身似浮云,心如飞絮,气若游丝.身像飘浮的云,心 ...

  6. hdu1852 Beijing 2008

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1852 题目大意: 求2008^n的所有因子和m对k取余,然后求2008^m对k取余. 解题思路: 首 ...

  7. Android(java)学习笔记37:String类型的面试题

    1. String类型面试题: package cn.itcast_02; /* * String s = new String(“hello”)和String s = “hello”;的区别? * ...

  8. Django:Django的路由系统

    一,URLconf配置 1,基本格式 from django.conf.urls import url urlpatterns = [ url(正则表达式, views视图函数,参数,别名), ] 2 ...

  9. About me and this site(2018/8/28)

    Name: 李秋豪 / [lǐ qiū háo] I'm a junior majoring in InfoSec at Harbin Institute of Technology. I'm int ...

  10. Android学习笔记_1_拨打电话

    1.首先需要在AndroidManifest.xml文件中加入拨打电话的权限,对应的配置文件: <?xml version="1.0" encoding="utf- ...