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. Hadoop ->> HIVE

    HIVE的由来: 最初由Facebook基于HDFS开发出来的一套数据仓库工具. HIVE可以干什么? HIVE可以将已经结构化的数据映射成一张表,然后可以使用HIVE语言像写T-SQL一样查询数据. ...

  2. 四、获取远程URL图片

    #!/usr/bin/python # -*- coding: UTF-8 -*- import re import urllib def getHtml(url): page = urllib.ur ...

  3. 二维数组展示到DataGridView(c#)

    窗体程序中二维数组展示到DataGridView public void TwoDArrayShowINDatagridview(string[,] arr) { DataTable dt = new ...

  4. codis学习

    一.codis-proxy 结构 1.Topology 2.Slots 3.ServerGroup 4.Server   二.codis-proxy 启动过程   1.初始化ProxyInfo Id ...

  5. C语言 宏的定义

    #include <stdio.h> // NUM叫做宏名 // 6是用来替换宏名的字符串 #define NUM 6 #define mul(a, b) ((a)*(b)) void t ...

  6. [原]Android打包之Eclipse打包

    Android自动打包流程详细图: 步骤一: 在工程中新建一个build.xml. 步骤二: 给工程配置ant工具. 选择ant工具的步骤如下: Windows->Shown view-> ...

  7. bootstrapTable refresh 方法使用简单举例

    本文就bootstrapTable refresh 方法如何传递参数做简单举例说明. 下面代码中,一个table,一个button,单击button会触发刷新表格操作. <!DOCTYPE ht ...

  8. What is Thread

    A thread is a fundamental unit of CPU utilization –a thread ID –a program counter –a register set –a ...

  9. hibermate一对一关联

    在hibernate.cfg.xml配置<mapping class="oneToOne.IDCard" />,以及实体类的get和set方法省略了. User类 @E ...

  10. WebNotes(PHP、css、JavaScript等)

    1. 数据库编码格式 gb-2312仅支持简体中文,GBK支持简体.繁体中文,utf-8通用程度最高. 2. HTTP请求方法 get方法请求时,会将传输的数据跟在链接后“显式地”发送,受限于链接长度 ...