1.文件I/O
一. open()&close()
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h>int main() {int fd;fd = open("abc.txt", O_CREAT|O_RDWR, 0777); // 若flag中使用了O_CREAT,則需要指定第三個參數(shù)訪問權(quán)限if (fd < 0)printf("file create failure");printf("current fd is: %d\n", fd);close(fd);return 0; }二.read()&write()
write.c
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>static const char *str = "http://www.baidu.com\n";int main() {int fd;fd = open("cde.txt", O_CREAT|O_RDWR|O_APPEND, 0777);write(fd, str, strlen(str));close(fd);return 0; }read.c
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <unistd.h> #include <stdio.h>int main() {char tmp[30];char str[1024]; int wr_fd;wr_fd = open("aaa.txt", O_CREAT|O_WRONLY|O_APPEND, 0777);int rd_fd;rd_fd = open("cde.txt", O_RDONLY);int total = 0, len;while((len = read(rd_fd, tmp, 30))) {strncpy(str+total, tmp, len);total+=len;}str[total-1] = '\0';close(wr_fd);close(rd_fd);printf("str is: %s\n", str);return 0; }運行結(jié)果:
str is: http://www.baidu.com
http://www.taobao.com
http://www.qq.com
http://www.dota2.com.cn
http://www.tmall.com
http://www.jd.com
http://www.apple.com
http://www.microsoft.com
三.lseek() 移動文件讀寫指針
使用lseek插入文件內(nèi)容
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h>static const char *str = "http://www.ibm.com\n";int main() {int fd;off_t offset;fd = open("cde.txt", O_RDWR);offset = lseek(fd, 0, SEEK_END);write(fd, str, strlen(str));close(fd); printf("cur offset is: %d\n", offset);return 0; }運行結(jié)果:
http://www.qq.com
http://www.dota2.com.cn
http://www.tmall.com
http://www.jd.com
http://www.apple.com
http://www.microsoft.com
http://www.ibm.com
http://www.ibm.com
使用lseek計算文件大小
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h>int main() {int fd;off_t offset;fd = open("cde.txt", O_RDWR);offset = lseek(fd, 0, SEEK_END);printf("cur file size is: %d\n", offset);close(fd);return 0; }運行結(jié)果:
cur file size is: 208
-rwxrwxr-x 1 yongdaimi yongdaimi 208 Jan 29 00:54 cde.txt
四.fcntl()
使用fcntl()獲得文件的flag標(biāo)志
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h>int main() {int fd;if ((fd = open("hh.txt", O_RDWR | O_CREAT | O_EXCL, 0664)) == -1) {perror("open error");exit(1);} int var;if ((var = fcntl(fd, F_GETFL, 0)) < 0) {perror("fcntl error");close(fd);exit(1);}switch(var & O_ACCMODE) {case O_RDONLY:printf("Read only ...\n");break;case O_WRONLY:printf("Write only ...\n");break;case O_RDWR:printf("Read And Write ...\n");break;default:printf("Do't know...\n");break;}if (var & O_APPEND) {printf("And Append...\n");}if (var & O_NONBLOCK) {printf("And Blocking...\n");}if (close(fd) == -1) {perror("close error");}return 0; }運行結(jié)果:
Read And Write ...
五.ioctl()
使用TIOCGWINSZ命令獲得終端設(shè)備的窗口大小
#include <sys/ioctl.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <unistd.h>int main() {struct winsize size;if (isatty(STDOUT_FILENO) == 0)exit(1);if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {perror("ioctl TIOCGWINSZ error");exit(1);}printf("%d rows, %d columns\n", size.ws_row, size.ws_col);return 0; }運行結(jié)果:
36 rows, 121 columns
?
總結(jié)
- 上一篇: Centos7 下安装配置tomcat7
- 下一篇: java开发简易计算器