文件操作(Linux系统编程)

创建一个目录时,系统会自动创建两个目录.和..

C语言实现权限控制函数

 #include<stdio.h>
 #include<stdlib.h>
 #include<sys/types.h>
 #include<sys/stat.h>

 int main(int argc , char **argv){
         int mode;
         int mode_u;
         int mode_g;
         int mode_o;
         char *path;

         ){
                 printf(]);
                 exit();
         }
                 //字符串转换成整型
         mode = atoi(argv[]);
          || mode < ){
                 printf("mode num error ! \n");
                 exit();
         }

         mode_u = mode / ;
         mode_g = (mode - (mode_u*)) / ;
         mode_o = mode - (mode_u*) - (mode_g*);
         mode = (mode_u *  * ) + (mode_g * ) + mode_o;
         path = argv[];
                 //改变权限函数
         ){
                 perror("chmod error");
                 exit();
         }

         ;
 }

 新建一个文件test.c
 命令:./程序   test.c

2.文件的输入输出函数

 #include<stdio.h>
 #include<sys/types.h>
 #include<sys/stat.h>
 #include<fcntl.h>
 #include<unistd.h>
 #include<errno.h>

 int main(){
         int fd;
                 //文件不存在创建文件
         ){
                //       if((fd = creat("example.c",S_IRWXU)) == -1){
               //        perror("open");
                         printf("open:%s with errno :%d \n", strerror(errno),errno);
                         exit();
                 }else{
                         printf("create file success\n");
                 }
                         //}

         close(fd);
         ;

 }

 my_creat.c: In function ‘main’:
 my_creat.c:: warning: incompatible implicit declaration of built-in function ‘exit’

3.文件的读写
文件读写和文件读写指针的移动操作

 #include<stdio.h>
 #include<sys/types.h>
 #include<sys/stat.h>
 #include<fcntl.h>
 #include<unistd.h>
 #include<errno.h>

 /**error handle function **/
 void my_err(const char *err_string , int line){
         fprintf(stderr,"line:%d",line);
         perror(err_string);
         exit();
 }
 /**read data function**/
 int my_read(int fd){
         int len;
         int ret;
         int i;
         ];
         /**get file length and save file-read-pointer to the head of file **/
          , SEEK_END) == -){
                 my_err("lseek",_LINE_);
         }
         ,SEEK_CUR)) == -){
                 my_err("lseek",_LINE_);
         }
         ,SEEK_SET)) == -){
                 my_err("lseek",_LINE_);
         }

         printf("len :%d\n",len);
         /**read data**/
         ){
                 my_err("read",_LINE_);
         }
         /**print data**/
          ; i < len ; i++){
                 printf("%c",read_buf[i]);
         }
         printf("\n");

         return ret;
 }
 int main(){
         int fd ;
         ] = "hello world!";
         /**create file in current dictionary**/
         ){
                 ){
                         my_err(,          Top
                }else{
                         printf("create file success!");
                 }
                 /*wirte data*/
                 if(wirte(fd,write_buf,strlen(write_buf)) != strlen(write_buf)){
                         my_err("write",_LINE_);
                 }
                 my_read(fd);

                 /**display file's space**/
                 printf("--------------------\n");
                 ,SEEK_END) == -){
                         my_err("lseek",_LINE_);
                 }

                 if(write(fd,write_buf,strlen(write_buf)) != strlen(write_buf)){
                         my_err("write",_LINE_);
                 }
                 my_read(fd);

                 close(fd);

                 ;
         }
 }

Linux C 程序 文件操作(Linux系统编程)(14)的更多相关文章

  1. 3)Linux程序设计入门--文件操作

    )Linux程序设计入门--文件操作 Linux下文件的操作 前言: 我们在这一节将要讨论linux下文件操作的各个函数. 文件的创建和读写 文件的各个属性 目录文件的操作 管道文件 .文件的创建和读 ...

  2. Linux学习记录--文件IO操作相关系统编程

    文件IO操作相关系统编程 这里主要说两套IO操作接口,各自是: POSIX标准 read|write接口.函数定义在#include<unistd.h> ISO C标准 fread|fwr ...

  3. Linux(三)__文件权限、系统的查找、文本编辑器

    一.文件权限 1.理解文件权限及其分配 2.掌握查看文件和目录的权限 3.掌握权限文字表示法和数值表示法 4.学会使用chmod命令设置权限 5.学会使用chown命令修改属主和组 linux文件能不 ...

  4. 5 个在 Linux 中管理文件类型和系统时间的有用命令

    对于想学习 Linux 的初学者来说要适应使用命令行或者终端可能非常困难.由于终端比图形用户界面程序更能帮助用户控制 Linux 系统,我们必须习惯在终端中运行命令.因此为了有效记忆 Linux 不同 ...

  5. 一、linux基础-对文件操作

    1.1文件夹创建-复制-移动-重命名-删除1.创建文件夹mkdir zjbdir 2.复制文件/文件夹复制文件到:当前目录cp -r zjbdir  zjbdir201600819复制文件到:当前目录 ...

  6. linux下的文件操作——批量重命名

    概述:在日常工作中,我们经常需要对一批文件进行重命名操作,例如将所有的jpg文件改成bnp,将名字中的1改成one,等等.文本主要为你讲解如何实现这些操作 1.删除所有的 .bak 后缀: renam ...

  7. Linux 目录和文件操作

    Linux常用命令--目录和文件操作 [目录]删除.复制.移动 : 1.删除文件夹用:rmdir 文件夹名 但是rmdir不能删除非空的文件夹,那如何删除非空文件夹呢: 2.通常情况下,删除文件用:r ...

  8. Linux下的文件操作——基于文件指针的文件操作(缓冲)

    目录操作 创建和删除目录: 原型为: #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> ...

  9. Linux中gz文件操作遇到的一些技巧和坑

    目录 不解压情况下获取gz超大文件的前/后几行? Perl读入gz文件操作? 不能直接通过wc -l 来统计gz文件的行数 前提是gz文件超大,如上百G,肯定不能直接解压来做. 不解压情况下获取gz超 ...

随机推荐

  1. Linux用户及用户组设置

    查看当前用户所属的组 groups  查看<user1>, <user2> 和 <user3>所属的组 groups <user1> <user2 ...

  2. codeigniter分页类传多个参数(转)

    http://example.com/index.php/控制器/方法名/页面的偏移值 页面的偏移值必须是方法名后第一个参数,否者分页类不能判断当前是哪一页,而用ci的分页类进行页面跳转时他是把偏移值 ...

  3. C++_归并排序(纯C版)

    #include <iostream> #include <stdlib.h> using namespace std; int compared(const void *ke ...

  4. Step-by-Step XML Free Spring MVC 3 Configuration--reference

    The release of Spring 2.5 reduce the burden of XML by introduction annotation based configuration, b ...

  5. IP101A芯片默认物理地址(PHY Adress)确定

    转载:http://blog.csdn.net/ropai/article/details/6961157 根据IP101A的DataSheet,芯片的第9,10,12,13,15脚为PHYAD0~P ...

  6. Linux内核初始化定义

    转载:http://blog.csdn.net/beatbean/article/details/8448623 1. Compile宏控制 位于include/linux/init.h /* The ...

  7. Java基础知识强化之网络编程笔记25:Android网络通信之 Future接口介绍(Java程序执行超时)

    1. Future接口简介 在Java中,如果需要设定代码执行的最长时间,即超时,可以用Java线程池ExecutorService类配合Future接口来实现. Future接口是Java标准API ...

  8. Debian 7 安装 Docker

    Debian 7更新内核到3.16后 一.添加docker源 在source.list中加入: # Docker Repo deb https://get.docker.io/ubuntu docke ...

  9. android源码编译常见错误

    错误1:You are attempting to build on a 32-bit system. Only 64-bit build environments are supported bey ...

  10. VMware系统运维(二)安装Microsoft .NET 3.5

    1.安装vmware vSphere 5.5 之前先安装Microsoft .NET 3.5. 2.安装Microsoft .NET3.5,添加角色和功能. 3.勾选.NET Framework 3. ...