溫馨提示×

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

小億
128
2023-07-11 00:46:41
欄目: 編程語言

在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",可以根據需要自行更改。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女