有没有帮人做机械设计的网站,徐州旅游的网站建设,品牌形象网站源码,深圳制作网站搜行者seo1.引言
为了学习linux系统下的app开发#xff0c;记载了学习文件编程的笔记
2.open函数 功能 打开一个文件 头文件 #includesys/stat.h #includefcntl.h 函数形式 int open(const char* pathname, int flags, mode_t mode); 返回值 如果调用成功#xff0c…1.引言
为了学习linux系统下的app开发记载了学习文件编程的笔记
2.open函数 功能 打开一个文件 头文件 #includesys/stat.h #includefcntl.h 函数形式 int open(const char* pathname, int flags, mode_t mode); 返回值 如果调用成功则返回文件描述符号标识文件资源后续会使用。 如果调用出错则会返回-1 参数 pathname打开的文件名含路径。 flags: 文件访问模式的bit mask。 mode: 文件权限模式 3. 使用案例
copy文件的案例
文件如下名字:copy_file.c
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include stdio.h
#include errno.h#define BUFFER_SIZE 1024 int main(int argc,char *argv[])
{ int from_fd,to_fd; int bytes_read,bytes_write; char buffer[BUFFER_SIZE]; char *ptr; //参数防卫命令选项必须输入3个参数if(argc!3) { fprintf(stderr,Usage:%s fromfile tofile/n/a,argv[0]); exit(1); } //以只读的方式打开第一个参数文件路径if((from_fdopen(argv[1],O_RDONLY))-1) { //打开失败fprintf(stderr,Open %s Error:%s/n,argv[1],strerror(errno)); exit(1); } // Constant Octal value Permission bit
// S_ISUID 04000 Set-user-ID
// S_ISGID 02000 Set-group-ID
// S_ISVTX 01000 Sticky
// S_IRUSR 0400 User-read
// S_IWUSR 0200 User-write
// S_IXUSR 0100 User-execute
// S_IRGRP 040 Group-read
// S_IWGRP 020 Group-write
// S_IXGRP 010 Group-execute
// S_IROTH 04 Other-read
// S_IWOTH 02 Other-write
// S_IXOTH 01 Other-execute// Flag describtion
// O_RDONLY Open for reading only v3
// O_WRONLY Open for writing only v3
// O_RDWR Open for reading and writing v3
// O_CLOEXEC Set the close-on-exec flag (since Linux 2.6.23) v4
// O_CREAT Create file if it doesn’t already exist v3
// O_DIRECT File I/O bypasses buffer cache
// O_DIRECTORY Fail if pathname is not a directory v4
// O_EXCL With O_CREAT: create file exclusively v3
// O_LARGEFILE Used on 32-bit systems to open large files
// O_NOATIME Don’t update file last access time on read() (since Linux 2.6.8)
// O_NOCTTY Don’t let pathname become the controlling terminal v3
// O_NOFOLLOW Don’t dereference symbolic links v4
// O_TRUNC Truncate existing file to zero length v3
// O_APPEND Writes are always appended to end of file v3
// O_ASYNC Generate a signal when I/O is possible
// O_DSYNC Provide synchronized I/O data integrity (since Linux 2.6.33) v3
// O_NONBLOCK Open in nonblocking mode v3
// O_SYNC Make file writes synchronousif((to_fdopen(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))-1) { fprintf(stderr,Open %s Error:%s/n,argv[2],strerror(errno)); exit(1); } while(bytes_readread(from_fd, buffer, BUFFER_SIZE)) { if((bytes_read-1)(errno!EINTR)) {break;} else if(bytes_read 0) { ptr buffer; while(bytes_writewrite(to_fd,ptr,bytes_read)) { if((bytes_write-1)(errno!EINTR))break; else if(bytes_writebytes_read)break; else if(bytes_write0) { ptrbytes_write; bytes_read-bytes_write; } } if(bytes_write-1)break; } } close(from_fd); close(to_fd); exit(0);
} 编译用Makefile
TARGET : app
#src file
SRC : copy_file.call:$(TARGET)echo make successfull$(TARGET): $(SRC)echo $(SRC)gcc $^ -I. -o $clean:rm $(TARGET).PHONY:all,cleanubuntu系统下使用gcc编译通过运行实例如下
终端命令./app copy_file.c a.c
结果展示命令 ll
total 41
drwxrwxrwx 1 root root 4096 May 25 23:05 ./
drwxrwxrwx 1 root root 0 Mar 8 21:31 ../
-rwxrwxrwx 1 root root 3165 May 25 23:05 a.c* ---复制成功的新文件
-rwxrwxrwx 1 root root 17056 May 25 23:04 app*
-rwxrwxrwx 1 root root 3165 May 25 23:04 copy_file.c*