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超 ...
随机推荐
- Scrapy 对不同的Item进行分开存储
在Piperlines里面进行对象的判断, def process_item(self, item, spider): if item.__class__ == BaseItem : #savexxx ...
- android152 笔记 4
42. Android中Task任务栈的分配. 首先我们来看下Task的定义,Google是这样定义Task的:a task is what the user experiences as an &q ...
- OSGi 学习(一)
从基础开始,先来说说OSGi的基本理念. OSGi通过隔离底层classloader,强制应用在设计的时候就考虑模块化,并且基于白板模式来支持服务的注册与订阅. 在OSGi中,模块可以等价理解为bun ...
- debian的备份与还原
不管是windows还是linux 备份都很重要 因为配好一个系统是要花很多时间的 出了问题的话 有备份直接还原就可以了 windows下有ghost 那么linux下呢?Linux可以在系统正在运行 ...
- Helpers\FastCache
Helpers\FastCache phpFastCache is a high-performance, distributed object caching system, generic in ...
- CentOS6.5 一键部署运行环境shell脚本
################################################## # CentOS6.5 ...
- Python Virtualenv 虚拟环境
在python2和python3共存的机器上做开发,要想互不干扰,虚拟环境很重要. Debian7 默认是python2.7.3,装好python3.4.1后怎么建立虚拟环境呢? $ pyvenv m ...
- NSURLConnection、NSURLSession
NSURLConnection 1.准备网络资源地址:URL 注意:由于URL支持26个英文字母,数字和少数的几个特殊字符. 因此对于URL中包含非标准URL的字符,需要进行编码. iOS提供了函 ...
- LeetCode 258
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 关于解决form表单记录上次保存填写记录清空
传进来的是一个HTML的Table的ID. function doReset(tableId){ var tbl_content = document.getElementById(tableId); ...