linux 文件编程
来源:互联网 发布:c语言强制转换 编辑:程序博客网 时间:2024/05/23 11:50
int creat(const char *filename, mode_t mode);
#filename:要创建的文件名
#mode:创建模式:只能控制创建用户的写属性,其余用户都是不可写,所以一般是755
a) S_IRUSR:可读 ->1
b) S_IWUSR:可写 ->2
c) S_IXUSR:可执行 ->
d) S_IRWXU:可读写执行 ->7
#返回值:成功返回0,不成功返回-1
应用举例
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void create_file(char *filename)
{
if(creat(filename,0755)<0)
{
printf("create file %s failed !\n",filename);
exit(1);
}
else
printf("create file %s successed !\n",filename);
}
int main(int argc,char *argv[])
{
int i;
/*参数检查,第一个参数是程序执行文件,第二个参数至第N个参数都是需要创建的文件名*/
if(argc<2){
perror("you have not input the filename!\n");
exit(1);
}
/*特别注意这是从1开始*/
for(i=1;i<argc;i++)create_file(argv[i]);
exit(0);
}
运行程序后,可以看到打印了这样的语句:
ivan@ivan-virtual-machine:~/123$ gcc file1.c -o file1
ivan@ivan-virtual-machine:~/123$ ./file1 hello1
create file hello1 successed !
通过运行命令ls -l可以看到当前目录的属性:
ivan@ivan-virtual-machine:~/123$ ls -l
总用量 56
-rwxrwxr-x 1 ivan ivan 7306 6月 25 14:55 a.out
-rwxrwxr-x 1 ivan ivan 7306 6月 25 14:58 file
-rwxr-xr-x 1 ivan ivan 7300 6月 25 17:49 file1
-rw-rw-r-- 1 ivan ivan 517 6月 25 17:49 file1.c
-rw-rw-r-- 1 ivan ivan 550 6月 25 14:58 file_create.c
-rwxrwxr-x 1 ivan ivan 7385 6月 25 15:47 file_open
-rw-rw-r-- 1 ivan ivan 524 6月 25 15:47 file_open.c
-rwxrwxr-x 1 ivan ivan 7159 6月 25 14:32 hello
-rwxr-xr-x 1 ivan ivan 0 6月 25 17:49 hello1
-rw-rw-r-- 1 ivan ivan 74 6月 25 14:30 hello.c
- 【Linux库函数编程】文件编程
- linux编程_Makefile文件
- linux编程_Makefile文件
- Linux文件编程实例
- linux文件编程
- linux文件编程
- Linux文件编程
- Linux文件编程
- Linux文件IO编程
- LINUX文件编程基础知识
- Linux文件编程
- linux文件编程
- linux文件编程
- linux 文件编程
- linux文件编程
- linux文件编程
- linux文件编程
- Linux文件编程
- QT窗口
- Linux中硬链接和软链接的区别
- Linux C++ 连接MySQL
- Python知识点
- 【怎样写代码】向现有类型“添加”方法 -- 扩展方法(一):扩展方法概述
- linux 文件编程
- Shiro 入门
- leetcode628. Maximum Product of Three Numbers
- Java中的类型转换
- 美团 CodeM 资格赛 Round A 合并回文子串
- 概率论的一些基本问题
- Linux学习基础篇(九)
- 序列选择器
- Mybatis自动映射