文件创建和打开
一、官方的解释说明图片
二、解释说明
预处理指令:
-
#include <sys/types.h>
:包含系统数据类型定义。 -
#include <sys/stat.h>
:包含文件属性定义。 -
#include <fcntl.h>
:包含文件控制定义,如open()、creat()等函数的声明。
函数原型:
-
int open(const char *pathname, int flags);
:打开一个文件,并返回其文件描述符。pathname是文件路径,flags指定打开文件的模式(如只读、只写、读写等)。 -
int open(const char *pathname, int flags, mode_t mode);
:这是open()的另一个版本,当文件不存在且flags包含O_CREAT时,会创建文件,并设置文件权限为mode。 -
int creat(const char *pathname, mode_t mode);
:这是open()的一个简化版本,专门用于创建新文件。pathname是文件路径,mode是文件权限。 -
int openat(int dirfd, const char *pathname, int flags);
和int openat(int dirfd, const char *pathname, int flags, mode_t mode);
:这两个函数与open()类似,但允许你指定一个打开或创建文件的相对路径的起始目录(由dirfd指定)。
三、写代码演示说明
这段代码的主要功能是尝试打开一个名为file1
的文件,如果文件不存在则创建它,并向该文件写入字符串"xuyadi niu!"
。接着,它会重新打开这个文件,读取先前写入的内容,并打印到控制台上。
- 头文件引入
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
这些头文件提供了程序所需的函数和类型定义,如文件操作、字符串操作、内存分配等。
- 主函数定义
int main()
{
...
}
程序的入口点。
- 变量定义
int fd;
char *buf = "xuyadi niu!";
定义了一个文件描述符fd
和一个字符串指针buf
,该指针指向一个字符串字面量。
- 尝试打开文件
fd=open("./file1",O_RDWR);
使用open
函数尝试以读写模式(O_RDWR
)打开名为file1
的文件。
- 检查文件是否打开成功
if(fd==-1){
...
}
如果fd
是-1,表示打开文件失败。
- 如果文件不存在,则创建它
printf("open file1 failed\n");
fd = open("./file1",O_CREAT|O_RDWR,0600);
if(fd>0){
printf("create file1 succeed!\n");
}
如果文件不存在,程序会尝试以读写模式并带有创建标志(O_CREAT|O_RDWR
)再次打开它,并设置文件权限为0600(只有文件所有者可以读写)。
- 写入文件
printf("open succeed fd=%d\n",fd);
int n_write = write(fd,buf,strlen(buf));
if(n_write!=-1){
printf("write %d byte to file\n",n_write);
}
使用write
函数将buf
指向的字符串写入文件。如果写入成功,则打印写入的字节数。
- 关闭文件
close(fd);
使用close
函数关闭文件。
- 重新打开文件以读取
fd=open("./file1",O_RDWR);
以读写模式重新打开文件以进行读取。
- 分配内存以读取文件内容
char *readBuf;
readBuf = (char*)malloc(n_write*sizeof(char)+1);
根据之前写入的字节数分配内存(并多分配一个字节用于字符串的终止符\0
)。
- 读取文件内容
int n_read =read(fd,readBuf,n_write);
printf("read %d,context:%s\n",n_read,readBuf);
使用read
函数从文件中读取内容,并打印读取的字节数和内容。
- 清空内存关闭文件并退出程序
free(readBuf);
close(fd);
return 0;
关闭文件并返回0,表示程序正常退出。
注意:
- 在使用
malloc
分配内存后,最好在不再需要这块内存时使用free
释放它,以避免内存泄漏。 - 如果在读取文件时文件内容小于之前写入的字节数(尽管在这种情况下不太可能),
read
函数可能会返回更少的字节数。这可能导致输出内容不完整或包含垃圾字符。在实际应用中,应该检查read
函数的返回值,并确保在输出之前为readBuf
添加终止符\0
。 - 在使用
open
函数时,最好总是检查返回值以确保文件已成功打开或创建。在创建文件时,还可以指定更多的标志和模式,以满足特定的需求。
代码注释:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd; // 文件描述符
char *buf = "xuyadi niu!"; // 要写入的字符串
// 尝试以读写模式打开文件
fd = open("./file1", O_RDWR);
if (fd == -1) {
printf("open file1 failed\n");
// 如果打开失败,则尝试以创建并读写模式打开文件
fd = open("./file1", O_CREAT | O_RDWR, 0600);
if (fd > 0) {
printf("create file1 succeed!\n");
}
}
printf("open succeed fd=%d\n", fd);
// 向文件写入字符串
int n_write = write(fd, buf, strlen(buf));
if (n_write != -1) {
printf("write %d byte to file\n", n_write);
}
// 关闭文件
close(fd);
// 重新以读写模式打开文件
fd = open("./file1", O_RDWR);
// 分配内存以读取文件内容
char *readBuf;
readBuf = (char*)malloc(n_write * sizeof(char) + 1); // 多分配一个字节用于字符串的终止符
if (readBuf == NULL) { // 检查内存是否成功分配
perror("malloc failed");
return 1; // 分配失败则退出程序
}
// 从文件读取内容
int n_read = read(fd, readBuf, n_write);
printf("read %d, context:%s\n", n_read, readBuf);
// 释放内存
free(readBuf);
// 关闭文件
close(fd);
return 0;
}