C语言之文件系统编程(1)

来源:互联网 发布:数组排序最快的方法 编辑:程序博客网 时间:2024/04/30 13:29

    在Linux中,为了更好地保护内核空间,程序的运行空间分为内核空间用户空间(也就是常称的内核态和用户态),它们分别运行在不同的级别上,在逻辑上是相互隔离的。因此,用户进程在通常情况下不允许访问内核数据,也无法使用内核函数,它们只能在用户空间操作用户数据,调用用户空间的函数。

1、文件打开:Openopen()系统调用可以打开或创建一个文件。#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);Returns file descriptor on success, or –1 on error        各参数及返回值的含义如下: pathname:要打开或创建的文件名称。 flags:标志位,指定打开文件的操作方式。 mode:指定新文件的访问权限。(仅当创建新文件时才使用该参数) 返回值:若成功返回文件描述符,否则返回-1并设置变量errno的值。代码:#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>int main(){//int fd = open("text", O_RDWR |  O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR);int fd = open("text.tex", O_RDWR |  O_CREAT, 0777);if(fd == -1){printf("文件打开失败\n");perror("open");}Print(“文件打开成功\n”);Close(fd);   //文件的关闭函数return 0;}
2文件读取:read
read()系统调用从打开的文件中读取数据。#include <unistd.h>ssize_t read(int fd, void *buf, size_t count);Returns number of bytes read, 0 on EOF, or –1 on error        各参数及返回值的含义如下: fd:要读取的文件的描述符。 buf:读取到的数据要放入的缓冲区。 count:要读取的字节数。 返回值:若成功返回读到的字节数,若已到文件结尾则返回0,若出错则返回-1并设置变量errno的值。注意:1. 这里的size_t是无符号整型,ssize_t是有符号整型。2. buf指向的内存空间必须事先分配好。代码:#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h>#include <erron.h>#define SIZE 1024int main(){int fd = open("LinkList.c", O_RDONLY);if(fd == -1){printf("文件打开失败\n");perror("open");return -1;}char buf[SIZE] = {0};int count = SIZE-1;char *p = buf;ssize_t red = 0;while(red = read(fd, p, count)){if(red == -1){if(erron == EAGAIN || erron == EINTR)  //文件读取被打断或者阻塞可以继续读取{continue;}break;}p += red;buf[red] = '\0';printf("%s",buf);}close(fd);return 0;}
3、文件写入:write
write()系统调用向打开的文件写数据。#include <unistd.h>ssize_t write(int fd, const void *buf, size_t count);Returns number of bytes written, or –1 on error        各参数及返回值的含义如下: fd:要写入的文件的描述符。 buf:要写入的数据所存放的缓冲区。 count:要写入的字节数。 返回值:若成功返回已写的字节数,出错则返回-1并设置变量errno的值。代码:#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h>#include <string.h>#define SIZE 1024int main(){int fd = open("abc", O_WRONLY | O_CREAT, 0777);if(fd == -1){printf("文件打开失败\n");perror("open");return -1;}char buf[SIZE] = {0};while(1){fgets(buf,0,);ssize_t ret = write(fd, buf, strlen(buf));if(ret == -1){printf("erron");break;}printf("ret = %d,buf = %s",ret, buf);}close(fd);return 0;}
4、文件偏移量:lseek
lseek系统调用可以改变文件偏移量(File Offset)。文件偏移量是一个整数,表示距文件起始处的字节数。#include <sys/types.h>#include <unistd.h>off_t lseek(int fildes, off_t offset, int whence);Returns new file offset if successful, or –1 on error    其中,参数whence必需是以下三个常量之一:SEEK_SET:将文件偏移量设置在距文件开始处offset个字节。SEEK_CUR:将文件偏移量设置在其当前值加offset,offset可正可负。SEEK_END:将文件偏移量设置为文件长度加offset,offset可正可负。代码:#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h>#define SIZE 1024int main(){int fd = open("text", O_RDWR, 0777);if(fd == -1){printf("文件打开失败\n");perror("open");}char *buf = "hello";lseek(fd, 10, SEEK_SET);write(fd, buf, 5);return 0;}



    

原创粉丝点击