Linux C 程序 文件操作(Linux系统编程)(14)
文件操作(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)的更多相关文章
- 3)Linux程序设计入门--文件操作
)Linux程序设计入门--文件操作 Linux下文件的操作 前言: 我们在这一节将要讨论linux下文件操作的各个函数. 文件的创建和读写 文件的各个属性 目录文件的操作 管道文件 .文件的创建和读 ...
- Linux学习记录--文件IO操作相关系统编程
文件IO操作相关系统编程 这里主要说两套IO操作接口,各自是: POSIX标准 read|write接口.函数定义在#include<unistd.h> ISO C标准 fread|fwr ...
- Linux(三)__文件权限、系统的查找、文本编辑器
一.文件权限 1.理解文件权限及其分配 2.掌握查看文件和目录的权限 3.掌握权限文字表示法和数值表示法 4.学会使用chmod命令设置权限 5.学会使用chown命令修改属主和组 linux文件能不 ...
- 5 个在 Linux 中管理文件类型和系统时间的有用命令
对于想学习 Linux 的初学者来说要适应使用命令行或者终端可能非常困难.由于终端比图形用户界面程序更能帮助用户控制 Linux 系统,我们必须习惯在终端中运行命令.因此为了有效记忆 Linux 不同 ...
- 一、linux基础-对文件操作
1.1文件夹创建-复制-移动-重命名-删除1.创建文件夹mkdir zjbdir 2.复制文件/文件夹复制文件到:当前目录cp -r zjbdir zjbdir201600819复制文件到:当前目录 ...
- linux下的文件操作——批量重命名
概述:在日常工作中,我们经常需要对一批文件进行重命名操作,例如将所有的jpg文件改成bnp,将名字中的1改成one,等等.文本主要为你讲解如何实现这些操作 1.删除所有的 .bak 后缀: renam ...
- Linux 目录和文件操作
Linux常用命令--目录和文件操作 [目录]删除.复制.移动 : 1.删除文件夹用:rmdir 文件夹名 但是rmdir不能删除非空的文件夹,那如何删除非空文件夹呢: 2.通常情况下,删除文件用:r ...
- Linux下的文件操作——基于文件指针的文件操作(缓冲)
目录操作 创建和删除目录: 原型为: #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> ...
- Linux中gz文件操作遇到的一些技巧和坑
目录 不解压情况下获取gz超大文件的前/后几行? Perl读入gz文件操作? 不能直接通过wc -l 来统计gz文件的行数 前提是gz文件超大,如上百G,肯定不能直接解压来做. 不解压情况下获取gz超 ...
随机推荐
- Metadata Lock原理6
一 简介 上一篇文章 <MetaData Lock 之一> 简单的介绍了MySQL 引入MDL 的前因后果,本文深入了解MDL的实现原理和运行机制.二 MDL 的类型 meta ...
- 日志管理 rsyslog服务浅析
http://www.xiaomastack.com/2014/11/13/rsyslog/
- SIGGRAPH 2014 之行
当地时间8月8号: 经历十个多小时的飞行,在紧急出口旁的位置上忍受发动机的轰鸣声后,顺利降落温哥华机场.回答完加拿大边检的几个诸如为何而来,打算住哪儿的问题后,比较顺利出关.三十五加元的打车费及百分十 ...
- Centos自动登录系统并自动打开VNC Server
系统自动登录 修改配置文件 sudo vim /etc/gdm/custom.conf 增加配置 [daemon] AutomaticLogin=spark AutomaticLoginEnable= ...
- 1、netlink 连接器 通信机制
使用netlink之前,先参考一下资料:http://www.ibm.com/developerworks/cn/linux/l-connector/ netlink通信机制介绍:资料来源 linux ...
- c语言指针难点
先来一个例子 例: #include "stdio.h" int main() { ] = {,,,,}; printf("a是一个地址%d\n",a); pr ...
- Entity Framework实体框架使用TrackerEnabledDbContext进行操作日志跟踪
在EF实体框架中进行日志跟踪,一般都是自己写个Log实体类,在数据保存时进行属性原始值验证来进行日志跟踪.当然还可以使用一些第三扩展库例如:entity framework extended进行日志记 ...
- MeasureSpec介绍
在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小,这个变量里面不仅有组件的尺寸大小,还有大小的模式. 这个大小的模式,有点难以理解.在系统 ...
- Call to undefined function imagettftext()解决方法
由 老高 发表于 2014-10-03 在 代码人生 分类 老高在一个新环境中装DEDECMS的时候发现后台验证码无法显示.直接搜索一下这个错误,有人说session错误,有的说权限错误等等,这不胡 ...
- python 元类——metaclass
from stack overflow:http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python Classes ...