c++中createfile函数的用法有哪些

1241
2023/7/7 0:46:41
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C++中,可以使用以下几种方法来创建文件:

  1. 使用ofstream类创建文件:
#include <fstream>
using namespace std;
int main() {
ofstream file("example.txt");
// 使用文件流进行文件操作
file.close();
return 0;
}
  1. 使用C风格的文件操作函数fopen来创建文件:
#include <cstdio>
int main() {
FILE* file = fopen("example.txt", "w");
// 使用文件指针进行文件操作
fclose(file);
return 0;
}
  1. 使用POSIX标准库中的open函数来创建文件:
#include <fcntl.h>
#include <unistd.h>
int main() {
int file = open("example.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
// 使用文件描述符进行文件操作
close(file);
return 0;
}

注意:以上示例中的文件名为"example.txt",可以根据需要自行更改。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: CreateFile的错误处理机制