简体   繁体   English

如何在C ++中使用变量创建文件?

[英]How do you create file with a variable in C++?

I was wondering how to put a variable where "example.txt" is below: 我想知道如何在下面的"example.txt"放置一个变量:

ofstream file;
file.open ("example.txt");

just put a variable there :-P 只是在这里放一个变量:-P

if you use std::string then you need to get the const char* using .c_str() so your options are: 如果使用std::string则需要使用.c_str()获取const char*因此您的选择是:

const char *filename = "example.txt"
file.open(filename);

or: 要么:

std::string filename = "example.txt"
file.open(filename.c_str());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM