文件操作——利用Linux系统调用

1.创建文件
int creat(const char *filename, mode_t mode)
filename:要创建的文件名(包含路径,缺省为当前路径)
mode:创建的文件的模式/访问权限
常见模式:
S_IRUSR 可读
S_IWUSR 可写
S_IXUSR 可执行
S_IRWXU 可读、可写、可执行
还可以直接使用数字来表示文件的访问权限:
可执行 1
可写 2
可读 4
上述三个值的和,如可读可写可执行 7(1+2+4)
无任何权限 0

 
  
1 #include < stdio.h >
2 #include < stdlib.h >
3
4 #include < sys / types.h >
5 #include < sys / stat.h >
6 #include < fcntl.h >
7
8   void create_file( char * filename){
9
10 /* 创建的文件的属性0755表示:
11 文件所有者可读可写可执行;
12 文件所有者所在组用户可读可执行;
13 其他用户可读可执行 */
14 if (creat(filename, 0755 ) < 0 ){
15 printf( " create file %s failure!\n " ,filename);
16 exit(EXIT_FAILURE);
17 } else {
18 printf( " create file %s success!\n " ,filename);
19 }
20 }
21
22 int main( int argc, char * argv[]){
23 int i;
24 if (argc < 2 ){
25 perror( " you haven't input the filename,please try again!\n " );
26 exit(EXIT_FAILURE);
27 }
28
29 for (i = 1 ;i < argc;i ++ ){
30 create_file(argv[i]);
31 }
32
33 exit(EXIT_SUCCESS);
34 }
35
2.打开文件
int open(const char *pathname, int flags)
int open(const char *pathname, int flags, mode_t mode)

常见打开标志(flags):
O_RDONLY 只读方式打开
O_WRONLY 只写方式打开
O_RDWR 读写方式打开
O_APPEND 追加方式打开
O_CREATE 创建一个文件
O_NOBLOCK 非阻塞方式打开
如果使用了O_CREATE标志,则必须指定mode来表示文件的访问权限。
 
  
1 #include < stdio.h >
2 #include < stdlib.h >
3
4 #include < sys / types.h >
5 #include < sys / stat.h >
6 #include < fcntl.h >
7
8 int main( int argc , char * argv[]){
9 int fd;
10 if (argc < 2 ){
11 puts( " please input the open file pathname!\n " );
12 exit( 1 );
13 }
14
15 // 如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,该文件的权限由第三个参数决定,此处为0755
16 // 如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打开的文件不存在,则会报错.
17 // 所以fd=open(argv[1],O_RDWR),仅仅只是打开指定文件
18 if ((fd = open(argv[ 1 ],O_CREAT | O_RDWR, 0755 )) < 0 ){
19 perror( " open file failure!\n " );
20 exit( 1 );
21 } else {
22 printf( " open file %d success!\n " ,fd);
23
24 }
25 close(fd);
26 exit( 0 );
27
28 }
3.关闭文件
int close(int fd)
fd:文件描述符(一个非负整数)
4.读文件
int read(int fd, const void *buf, size_t length)
从文件描述符fd所指向的文件中读取length个字节到buf所指向的缓冲区中,返回实际读取的字节数。
5.写文件
int write(int fd, const void *buf, size_t length)
把buf所指向的缓冲区中的length个字节写到文件描述符fd所指向的文件中,返回实际写入的字节数。
6.定位文件(移动文件指针)
int lseek(int fd, offset_t offset, int whence)
将文件读写指针相对whence移动offset个字节。操作成功,则返回文件指针相对于文件头的位置。
whence可以取下述值:
SEEK_SET:相对文件开头
SEEK_CUR:相对文件读写指针的当前位置
SEEK_END:相对文件末尾
offset可取负值,表示向前移动。
7.访问权限判断
int access(const char *pathname, int mode)
pathname:文件名称
mode:要判断的访问权限,可以取下面的值或者它们的组合。R_OK:文件可读,W_OK:文件可写,X_OK:文件可执行,F_OK:文件存在。
返回值:当我们测试成功时,函数返回0,否则如果有一个条件不符合,返回-1。
 
  
1 #include < sys / types.h >
2 #include < sys / stat.h >
3 #include < fcntl.h >
4 #include < stdio.h >
5 #include < errno.h >
6
7 #define BUFFER_SIZE 1024
8
9 int main( int argc, char ** argv)
10 {
11 int from_fd,to_fd;
12 int bytes_read,bytes_write;
13 char buffer[BUFFER_SIZE];
14 char * ptr;
15
16 if (argc != 3 )
17 {
18 fprintf(stderr, " Usage:%s fromfile tofile/n/a " ,argv[ 0 ]);
19 exit( 1 );
20 }
21
22 /* 打开源文件 */
23 if ((from_fd = open(argv[ 1 ],O_RDONLY)) ==- 1 )
24 {
25 fprintf(stderr, " Open %s Error:%s/n " ,argv[ 1 ],strerror(errno));
26 exit( 1 );
27 }
28
29 /* 创建目的文件 */
30 if ((to_fd = open(argv[ 2 ],O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR)) ==- 1 )
31 {
32 fprintf(stderr, " Open %s Error:%s/n " ,argv[ 2 ],strerror(errno));
33 exit( 1 );
34 }
35
36 /* 以下代码是一个经典的拷贝文件的代码 */
37 while (bytes_read = read(from_fd,buffer,BUFFER_SIZE))
38 {
39 /* 一个致命的错误发生了 */
40 if ((bytes_read ==- 1 ) && (errno != EINTR)) break ;
41 else if (bytes_read > 0 )
42 {
43 ptr = buffer;
44 while (bytes_write = write(to_fd,ptr,bytes_read))
45 {
46 /* 一个致命错误发生了 */
47 if ((bytes_write ==- 1 ) && (errno != EINTR)) break ;
48 /* 写完了所有读的字节 */
49 else if (bytes_write == bytes_read) break ;
50 /* 只写了一部分,继续写 */
51 else if (bytes_write > 0 )
52 {
53 ptr += bytes_write;
54 bytes_read -= bytes_write;
55 }
56 }
57 /* 写的时候发生的致命错误 */
58 if (bytes_write ==- 1 ) break ;
59 }
60 }
61 close(from_fd);
62 close(to_fd);
63 exit( 0 );
64 }
65

 

转载于:https://www.cnblogs.com/mikelin/archive/2010/07/15/1777956.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值