在 C++ 中创建文件,就像按右键单击>新建>新建文本文档时一样
create a file in c++ like when you press right-click->new->new text document
在某个目录中用键盘读取的某个变量的名称创建文件的C/C++命令是什么?例如:以前从键盘上读取的名称为John的文本文件,程序将创建文件John.txt
只需这样做,在本例中,我使用键盘询问文件名,然后使用fopen
创建文件,并将其传递给用户编写的文件名。
#include <stdio.h>
#include <string.h>
int main(){
FILE *f;
char filename[40], path[255];
strcpy(path,"folder/path/"); //copies the folder path into the variable
printf("Insert your filenamen");
scanf("%s",&filename);
strcpy(path,filename);
f = fopen(path,'w'); //w is for writing permission
//Your operations
fclose(f);
return 0;
}
下面是另一个使用POO的例子,它对C++更好:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string path = "my path";
string filename;
cout << "Insert your filename" << endl;
cin >> filename;
path = path + filename;
ofstream f;
f.open (path.c_str()); //Here is your created file
//Your operations
f.close();
return 0;
}
p.D:这个例子使用Unix的路径。
C方式:FILE* f = fopen(filename,"w");
(假设您想写入它,否则第二个参数是"r"。)
C++方式:std::fstream f(filename,std::ios::out);
(假设你想写,为了读取它是std::ios::in)
此外,在提出此类问题之前,请尝试搜索C/C++文档。下次查看此网站。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何在文档文件夹中创建目录
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 关于类的 Python 文档 - 对C++的引用不正确
- 解析文本文档并在 C++ 中打印
- 返回的内容以及本文档描述的功能中所采取的措施
- 我想串入文本文档中列出的电话号码
- C++文本文档中写入新行并按索引读取
- C 文本文档
- C++ 保存文本文档
- 如何将文本文档信息(名称和数字)写入多个数组
- 代码块输入文本文件数组错误
- 在 C++ 中创建文件,就像按右键单击>新建>新建文本文档时一样
- 在c++中从多行文本文档中读取一个数字
- C++ 2010中的文本文件数组
- 如何在c++中使用cin将带有空格的文本插入到文本文档中
- 从文本文档中输入字符串作为参数
- C++文本文档中的行索引
- 在文本文档中查找特定行