文件创建和打开

文件创建和打开

一、官方的解释说明图片

在这里插入图片描述

二、解释说明

预处理指令

  • #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!"。接着,它会重新打开这个文件,读取先前写入的内容,并打印到控制台上。

  1. 头文件引入
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

这些头文件提供了程序所需的函数和类型定义,如文件操作、字符串操作、内存分配等。

  1. 主函数定义
int main()
{
    ...
}

程序的入口点。

  1. 变量定义
int fd;
char *buf = "xuyadi niu!";

定义了一个文件描述符fd​和一个字符串指针buf​,该指针指向一个字符串字面量。

  1. 尝试打开文件
fd=open("./file1",O_RDWR);

使用open​函数尝试以读写模式(O_RDWR​)打开名为file1​的文件。

  1. 检查文件是否打开成功
if(fd==-1){
    ...
}

如果fd​是-1,表示打开文件失败。

  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(只有文件所有者可以读写)。

  1. 写入文件
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​指向的字符串写入文件。如果写入成功,则打印写入的字节数。

  1. 关闭文件
close(fd);

使用close​函数关闭文件。

  1. 重新打开文件以读取
fd=open("./file1",O_RDWR);

以读写模式重新打开文件以进行读取。

  1. 分配内存以读取文件内容
char *readBuf;
readBuf = (char*)malloc(n_write*sizeof(char)+1);

根据之前写入的字节数分配内存(并多分配一个字节用于字符串的终止符\0​)。

  1. 读取文件内容
int n_read =read(fd,readBuf,n_write);
printf("read %d,context:%s\n",n_read,readBuf);

使用read​函数从文件中读取内容,并打印读取的字节数和内容。

  1. 清空内存关闭文件并退出程序
free(readBuf);
close(fd);
return 0;

关闭文件并返回0,表示程序正常退出。

注意

  1. 在使用malloc​分配内存后,最好在不再需要这块内存时使用free​释放它,以避免内存泄漏。
  2. 如果在读取文件时文件内容小于之前写入的字节数(尽管在这种情况下不太可能),read​函数可能会返回更少的字节数。这可能导致输出内容不完整或包含垃圾字符。在实际应用中,应该检查read​函数的返回值,并确保在输出之前为readBuf​添加终止符\0​。
  3. 在使用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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值