使用C Visual Studio中的循环写入不同的文件名
writing to different file names using for loop in C++ visual studio
我正在尝试将一些数据写入每个索引的不同文件。即,文件名应该从datafile0.res更改为datafile99.res。取决于传递的索引。
我正在使用Visual C 2008。
代码是:
void write_data(int index) {
ofstream coutput;
// first i need to convert integer index to string. Not sure if right?
ostringstream temp;
temp<<index;
std::string s = temp.str();
std::string dir = "C:My_Datadatafile" + s + ".res";
coutput.open(dir);
当我运行此代码时,出现以下错误:
error C2664: 'void std::basic_ofstream<_Elem,_Traits>::open(const wchar_t ,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.
请需要您的帮助。
预先感谢
这与您所陈述的目标或串联无关。
std::fstream::open
,在C 11之前,需要const char*
,而不是std::string
。
so:
coutput.open(dir.c_str());
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- 从命令行c++发送文本文件名
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- 循环并将数字的索引用作文件名
- 使用C Visual Studio中的循环写入不同的文件名
- 如何使用 for 循环多次提示用户输入不同的文件名
- 这个函数请求用户通过循环输入文件名的错误在哪里?
- 从ofstream的文件名括号内输入代码值(如i从循环中输入)的c++语法