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超 ...
随机推荐
- Haproxy+MYSQL 负载均衡 原创
[root@monitor app1]# yum install haproxy Loaded plugins: security : epel | : epel/primary_db | : ext ...
- mysql高效获取两张表共同字段的交集数据
例如下面两站表A,B.A表和B表分别有5-10w数据.A表结构如下:id bid name title publisher extraB表结构如下id bid name title publisher ...
- Windows 之 获取管理员权限
新建文本文档,写入如下内容: Windows Registry Editor Version 5.00 [-HKEY_CLASSES_ROOT\*\shell\runas] [HKEY_CLASSES ...
- Collections.synchronizedMap 详解
众所周知,HashMap 本身非线程安全的,但是当使用 Collections.synchronizedMap(new HashMap()) 进行包装后就返回一个线程安全的Map. 怎么实现的呢?今天 ...
- Server Tomcat v7.0 Server at localhost failed to start.错误可能原因之一
故事由来: 学Servlet中,突然发现启动Tomcat时出现这个问题 故事梗概: 学习servlet尽然能碰到服务器起不来的,百思不得其解,然后我百度,我FQgoogle,找stackoverflo ...
- 编译APK时出现 This attribute must be localized 的两种解决方法 免修改xml
下面两种方法可以让你不需要修改APP XML字符串的条件下忽略 This attribute must be localized 的 Android 源码编译时的错误警告. 1.修改当前APP的 An ...
- s实现指定时间自动跳转到某个页面
--js实现指定时间自动跳转到某个页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- javaweb学习总结二十三(servlet开发之线程安全问题)
一:servlet线程安全问题发生的条件 如果多个客户端访问同一个servlet时,发生线程安全问题,那么它们访问的是相同的资源.如果访问 的不是相同资源,则不存在线程安全问题. 实例1:不会产生线程 ...
- hdu 4284 深度优先搜索
思路:就是找能走的走,遍历一边所有情况,满足就退出. Accepted 4284 328MS 2280K 2239 B C++ //#pragma comment(linker, "/STA ...
- IOS图片的两种处理:加阴影和变灰色
#import <QuartzCore/QuartzCore.h> //图片阴影 UIImageView *img; [[img layer] setShadowOffset:CGSize ...