二、 系统调用文件类
2.1.打开文件: int open(const char *pathname,int flags);
int open(const char *pathname,int flags,mode_t mode)
打开或者创建一个文件
所属头文件: sys/types.h sys/stat.h fnctl.h
返回值: 成功:文件描述符 失败:-1
参数说明: pathname: 要打开的文件名(含路径)
flags: 文件打开标志
-O_APPEND:以追加方式打开文件
-O_CREAT:当打开的文件不存在的时候,创建该文件
mode:一定是在flags中使用了O_CREAT标志,mode记录待创建文件的访问权限.
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
-
- void main()
- {
- int fd;
- char *pathname = "./test.c";
- fd = open(pathname, O_RDWR|O_CREAT, 00666);
-
- if(fd <0)
- printf("open file fail!\n");
-
- }
2.2.创建文件: int creat (const char *pathname,mode_t mode);
创建一个文件,并以只写的方式打开该文件
所属头文件: sys/types.h sys/stat.h fcntl.h
返回值: 成功:只写打开的文件描述符 失败:-1
参数说明: Pathname:要创建的文件名(含路径)
Mode:创建文件的读写权限.
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
-
- void main()
- {
- int fd;
- char *pathname = "./test.c";
-
- fd = creat(pathname, 0666);
-
- if(fd<0)
- {
- printf("file creat failed!\n");
- }
-
- }
2.3.关闭文件: Int close(int filedes);
关闭一个打开的文件,释放进程在文件上的记录锁
所属头文件: unistd.h
返回值: 成功:0 出错-1
参数说明: Filedes:待关闭的文件描述符
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
-
-
- void main()
- {
- int fd;
- int ret;
- char *pathname = "./test.c";
-
- fd = open(pathname, O_RDWR|O_CREAT, 0666);
-
- if(fd<0)
- printf("file open fialed!\n");
-
- ret = close(fd);
- if(ret<0)
- printf("file close failed!\n");
- }
2.4.读文件: ssize_t read(int fd, void *buf, size_t count);
从打开文件中读数据
所属头文件: unistd.h
返回值: 成功:返回读到的字节数.若已到文件结尾返回0,失败-1
参数说明: fd:要读取数据的文件描述符
count:要读取的字节数
buf:指向来的数据存到buf指向的空间
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
-
- void main()
- {
- int fd;
- char *pathname = "./test.c";
- size_t count = 10;
- size_t read_ret;
- char buf[20];
-
- fd = open(pathname, O_RDWR|O_CREAT, 0666);
- if(fd<0)
- printf("file open failed!\n");
-
- read_ret = read(fd,buf,count);
- if(read_ret <0)
- printf("file read failed!\n");
- else
- {
- buf[count] = '\0';
- printf("the buf is %s\n",buf);
- }
-
- close(fd);
- }
2.5.写文件: ssize_t write(int fd, const void *buf, size_t count);
向打开的文件写数据
所属头文件: unistd.h
返回值: 成功:返回已写的字节数,出错:-1
参数说明: fd:要写入数据的文件的描述符
buf:要写入的数据的存放位置
count:希望写入的字节数
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
-
-
- void main()
- {
- int fd;
- char *buf = "this is my string.";
- size_t count = 18;
- ssize_t write_ret;
-
- fd = open("./test.c",O_RDWR|O_CREAT, 0666);
- if(fd < 0)
- printf("file open failed\n");
-
- write_ret = write(fd, buf, count);
- if(write_ret < 0)
- printf("file write failed\n");
-
- close(fd);
- }
2.6.定位文件: off_t lseek(int fd, off_t offset, int whence);
重新定位文件读写位置
所属头文件: sys/types.h unistd.h
返回值: 成功:返回移动后的文件指针,距离文件头的位置. 失败:-1
参数说明: fd:文件描述符
offset:便宜数
Whence:起始位置
SEEK_SET:从文件开始处offset个字节
SEEK_CUR:从当前偏移量加offset
SEEK_END:从文件长度加offset,可以为正负
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
-
- void main()
- {
- int fd;
- char *pathname = "./test.c";
- char *w_buf = "this is my string.";
- char r_buf[20];
- size_t count = 18;
- ssize_t w_ret;
- ssize_t r_ret;
- off_t ls_ret;
-
- fd = open(pathname, O_RDWR|O_CREAT, 0666);
- if(fd < 0)
- printf("file open fialed!\n");
-
- w_ret = write(fd, w_buf, count);
- if(w_ret < 0)
- printf("file write fialed!\n");
-
- ls_ret = lseek(fd, 0, SEEK_SET);
- if(ls_ret < 0)
- printf("file lseek fialed!\n");
-
- r_ret = read(fd, r_buf, 10);
- if(r_ret < 0)
- printf("file read fialed!\n");
- else
- {
- r_buf[10] = '\0';
- printf("file read is %s\n", r_buf);
- }
-
- close(fd);
- }
2.7.复制文件描述符: int dup(int oldfd);
int dup2(int oldfd, int newfd);
复制一个文件描述符
所属头文件: unistd.h
返回值: 成功:返回新的文件描述符 失败:-1
参数说明: oldfd:需要复制的文件描述符
Newfd:新文件描述符
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
-
- void main()
- {
- int fd;
- int fd1;
- char *pathname = "./test.c";
- char *w_buf = "this is my string.";
- char r_buf[20];
- size_t count = 18;
- ssize_t w_ret;
- ssize_t r_ret;
- off_t ls_ret;
-
- fd = open(pathname, O_RDWR|O_CREAT, 0666);
- if(fd < 0)
- printf("file open failed!\n");
-
- fd1 = dup(fd);
-
- w_ret = write(fd1, w_buf, count);
- if(w_ret < 0)
- printf("file write fialed!\n");
-
- ls_ret = lseek(fd1, 0, SEEK_SET);
- if(ls_ret < 0)
- printf("file lseek fialed!\n");
-
- r_ret = read(fd1, r_buf, 10);
- if(r_ret < 0)
- printf("file read fialed!\n");
- else
- {
- r_buf[10] = '\0';
- printf("file read is %s\n", r_buf);
- }
-
- close(fd1);
- }
点击(此处)折叠或打开
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
-
- void main(int argc, char *argv[])
- {
- int fd_s;
- int fd_t;
- int count = 0;
- char buf[512];
-
- if(argc != 3)
- {
- printf("usage: filecp [srcfile] [detfile]\n");
- return;
- }
-
- /*1.打开源文件*/
- fd_s = open(argv[1], O_RDONLY);
-
- /*2.打开目标文件*/
- fd_t = open(argv[2], O_RDWR|O_CREAT, 0666);
-
- /*3.读取源文件数据*/
- while((count = read(fd_s, buf, 512)) > 0)
- {
- write(fd_t, buf, count); /*4.将数据写入目标文件*/
- }
-
- /*5.关闭文件*/
- close(fd_s);
- close(fd_t);
- }