linux系统编程-文件与IO(一)
2014-04-29 13:47
330 查看
1.文件描述符与文件指针
/* 文件指针 FILE* fp 文件描述符 int STDIN_FILENO stdin SDTOUT_FILENO stdout STDERR_FILENO stderr fileno 文件指针->文件描述符 fdopen 文件描述符->文件指针 */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { printf("fileno(stdin)= %d\n",fileno(stdin)); return 0; }2.文件的系统调用
close、open、creat、read、write
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> //#define ERR_EXIT(m) (perror(m),exit(EXIT_FAILURE)) #define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ }while(0) int main(int argc, char *argv[]) { int fd; fd = open("test.txt",O_RDONLY); /* if(fd == -1) { fprintf(stderr,"open error!errorno is %d,%s\n",errno,strerror(errno)); exit(EXIT_FAILURE); } */ /* if(fd == -1) { perror("open error!\n"); exit(EXIT_FAILURE); } */ if(fd == -1) ERR_EXIT("open error!"); printf("open succ\n"); return 0; }
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ }while(0) int main(int argc, char *argv[]) { umask(0); int fd; fd = open("test.txt",O_WRONLY | O_CREAT,0666); if(fd == -1) ERR_EXIT("open error!"); printf("open succ\n"); close(fd); return 0; }
相关文章推荐
- linux系统编程之文件与IO(二):系统调用read和write
- linux系统编程之文件与IO:文件描述符、open,close
- linux系统编程之文件与IO:利用lseek()创建空洞文件
- Linux系统编程(1) —— 文件IO
- linux系统编程之文件与IO:目录访问相关系统调用
- Linux系统编程--文件IO操作
- 三、Linux系统编程-文件和IO(一)文件的打开和关闭
- Linux系统编程(2)——文件与IO之系统调用与文件IO操作
- Linux系统编程(4)——文件与IO之ioctl函数
- 嵌入式 Linux系统编程(一)——文件IO
- Linux系统编程(2)——文件与IO之系统调用与文件IO操作
- Linux系统编程(3)——文件与IO之fcntl函数
- linux系统编程之文件与IO(七):时间函数小结
- linux系统编程之文件与IO(三):利用lseek()创建空洞文件
- linux系统编程之文件与IO:实现ls -l功能
- Linux系统编程(4)——文件与IO之ioctl函数
- linux系统编程之文件与IO(五):stat()系统调用获取文件信息
- linux系统编程之文件与IO:时间函数小结