1、基本I\O操作函数
int open(const char* pathname, int flags, .../* mode_t mode */);
int close(int fd);
ssize_tread(int fd, void *buf, size_t count);
ssize_t write(int fd, void *buf, size_t count);
int create(const char* pathname, mode_t mode); --等效于int open(const char* pathname, O_WRONLY| O_CREATE | O_TRUNC, mode_t mode);
off_t lseek(int fds, off_t offset, int whence); --whence: SEEK_SET、SEEK_CUR、SEEK_END
例子1:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #define PERMS 0666 #define DUMMY 0 #define BUFSIZE 1024 int main(int argc, char *argv[]) { int source_fd, target_fd, num; #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #define PERMS 0666 #define DUMMY 0 #define BUFSIZE 1024 int main(int argc, char *argv[]) { int source_fd, target_fd, num; char iobuffer[BUFSIZE]; if (argc != 3) { printf("Usage: copy Sourcefile Targetfile\n"); return 1; } if ( (source_fd = open(*(argv+1), O_RDONLY, DUMMY)) == -1) { printf("Source file open error!\n"); return 2; } if ( (target_fd = open(*(argv+2), O_WRONLY|O_CREAT, PERMS)) == -1) { printf("Target file open error!\n"); return 3; } while ( (num = read(source_fd, iobuffer, BUFSIZE)) > 0) if (write(target_fd, iobuffer, num) != num) { printf("Target file wirte error!\n"); return 4; } close(source_fd); close(target_fd); return 0; } |
例子2:
#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #define LNAME 9 #define PERMS 0666 #define DATAFILE "datafile" #define USERS 10 #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #define LNAME 9 #define PERMS 0666 #define DATAFILE "datafile" #define USERS 10 typedef struct user { int uid; char login[LNAME+1]; } RECORD; char *user_name[] = {"u1","u2","u3","u4","u5","u6","u7","u8","u9","admin"}; int main() { int i, fd; RECORD rec; if ( (fd = open(DATAFILE, O_WRONLY | O_TRUNC | O_CREAT, PERMS)) == -1) { perror("open"); return 1; } for (i = USERS - 1; i >= 0; i--) { rec.uid = i; strcpy(rec.login, user_name[i]); lseek(fd, (long)i * sizeof(RECORD), SEEK_SET); write(fd, (char *)&rec, sizeof(RECORD)); } lseek(fd, 0L, SEEK_END); close(fd); return 0; } |
2、文件高级操作
1)确定和改变文件模式
mode_t umask(mode_t cmask);--改变进程文件创建屏蔽值函数
int chmod(const char * filename, mode_t mode);
int fchmod(int fd, mode_t mode);
int chown(const char * filename, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int rename(const * oldname, const char * newname);
int truncate(char *pathname, size_t len);
int truncate(int fd, size_t len);
int access(const char * pathname, int mode);
2) 查询文件信息
int utime(const char * pathname, const struct utimebuf *times)
utimebuf 结构:time_t action、time_t modtime
int utimes(const char * pathname, const struct timeval values[2]);
timeval结构: long tv_sec、long tv_usec <sys/time.h>
#include <sys/types.h> #include <utime.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> int main(int argc, char *argv[]) { int i, fd; struct stat statbuf; struct utimbuf times; if ( argc != 2) { printf("Usage: a filename \n"); return 1; } if ( (fd = open(argv[1], O_RDWR)) < 0) { printf("%s open failed\n", argv[1]); return 3; } if (stat(argv[1], &statbuf) < 0) return 2; if (ftruncate(fd, 0) < 0) { printf("%s truncate failed\n", argv[1]); return 4; } printf("%s is truncated now.\n", argv[1]); times.actime = statbuf.st_atime; times.modtime = statbuf.st_mtime; if (utime(argv[1], ×) == 0) { printf("utime() call successful \n"); } else printf("Error:utime() call failed. \n"); return 0; } |
int stat(char * pathname, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(char * pathname, struct stat *buf);
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, char *argv[]) { int i; struct stat buf; char *ptr; for (i = 1; i < argc; i++) { printf("%s:", argv[i]); if (lstat(argv[i], &buf) < 0) { printf("error!\n"); continue; } switch(S_IFMT&buf.st_mode) { case S_IFREG: ptr = "regular"; break; case S_IFDIR: ptr = "directory"; break; case S_IFCHR: ptr = "character special"; break; case S_IFBLK: ptr = "block special"; break; //case S_IFFIFO: ptr = "fifo"; break; //case S_IFSLNK: ptr = "symbolic link"; break; case S_IFSOCK: ptr = "socket"; break; default: ptr = "unknown mode"; } printf("%s\n", ptr); } return 0; } |
3)文件其他操作
int dup(int oldfd);
int dup2(int oldfd, int newfd);
int fcntl(int fd, int cmd, int arg);