Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 制作文件的顺序列表_C++ - Fatal编程技术网

C++ 制作文件的顺序列表

C++ 制作文件的顺序列表,c++,C++,我已经在一个问题上纠缠了一段时间,似乎找不到答案。 我试图创建多个同名文件,但每次结尾都有不同的编号,我刚开始尝试使用 int seq_number = 1; while (seq_number < 10) { ofstream fsave; fsave.open("filename" + seq_number + ".txt"); fsave << "blablabla"; fsave.close(); seq_number = seq_number + 1; } int

我已经在一个问题上纠缠了一段时间,似乎找不到答案。 我试图创建多个同名文件,但每次结尾都有不同的编号,我刚开始尝试使用

int seq_number = 1;
while (seq_number < 10)
{
ofstream fsave;
fsave.open("filename" + seq_number + ".txt");
fsave << "blablabla";
fsave.close();
seq_number = seq_number + 1;
}
int seq_number=1;
而(序号<10)
{
流fsave;
打开(“文件名”+序号+“.txt”);
fsaveReplace

fsave.open(filename + ".txt");

这是因为流的
构造函数将
字符常量*
作为参数,而不是
std::string


此外,您的第一个版本生成奇怪的文件名,因为在C和C++中,将一个整数添加到 char */COD>只在字符数组内偏移。它不附加到字符串。

< P> >可以这样做:

ostringstream s;
s << "character" << seq_number << ".txt";
fsave.open(s.str());
fsave << "blabla";
fsave.close();
fsave.open("filename" + seq_number);
#include <iostream>
#include <string>
#include <sstream>

int main (int argc, char const* argv[])
{
    std::string filename;
    int seq_number = 10;
    filename = "character";
    std::stringstream s;
    s << filename << seq_number << ".txt";
    filename = s.str();
    std::cout<< filename << std::endl; // <-- Here open your file instead print the filename
}
ostringstreams;
s
这是错误的;您没有构造新变量(
filename
已构造),这里需要的是赋值

filename = s.str();
那么

(不过,如果您使用的是C++11,则无需进行此更改)

尽管如此,我个人还是会用流来构建整个文件名:

ostringstream s;
s<<"character"<<seq_number<<".txt";
fsave.open(s.str.c_str());
这甚至不应该编译,因为您正在将一个整数求和为
常量字符*
(从而移动“字符串的开头”),然后将其再次求和为
常量字符*
,这是完全不允许的。如果是这样,可能可以编译:

ostringstream s;
s << "character" << seq_number << ".txt";
fsave.open(s.str());
fsave << "blabla";
fsave.close();
fsave.open("filename" + seq_number);
#include <iostream>
#include <string>
#include <sstream>

int main (int argc, char const* argv[])
{
    std::string filename;
    int seq_number = 10;
    filename = "character";
    std::stringstream s;
    s << filename << seq_number << ".txt";
    filename = s.str();
    std::cout<< filename << std::endl; // <-- Here open your file instead print the filename
}

但它不会给出要求的结果-<代码>文件名“< /Cord>”是一个指针(不是C++字符串),因此求一个整数就只移动给定偏移的指针。


在第二个代码段中,您使用的是一个对象(
filename
),因为它是一个函数,只有在类重载
operator()时才允许使用该函数因此,编译器抱怨在该对象上不允许这样的操作。C++中的

< P>不能将<代码> int >代码转换为字符串,或者将它连接到一个字符串上,而不是将它链接到胡'*:

"filename" + seq_number + ".txt"
^const char*    ^int      ^const char*
另外,
ostream
无法将文件名作为
字符串接收,它必须是
const char*
,您可以通过“c_str()”临时获取

使用
sprintf
ostringstream
(正如您所做的那样)或C++11
来使用字符串来执行以下操作:

#include <string>
#include <iostream>
int main() {
    for(int seq_number = 1; i<10; ++i) {
        std::string num_as_string = std::to_string(seq_number);  // make a string, C++11
        std::string filename = "abcd" + num_as_string + ".txt";
        std::ostream f(filename.c_str());
        f << "text\n";
   }
}
#包括
#包括
int main(){

对于(int seq_number=1;i您可以这样做:

ostringstream s;
s << "character" << seq_number << ".txt";
fsave.open(s.str());
fsave << "blabla";
fsave.close();
fsave.open("filename" + seq_number);
#include <iostream>
#include <string>
#include <sstream>

int main (int argc, char const* argv[])
{
    std::string filename;
    int seq_number = 10;
    filename = "character";
    std::stringstream s;
    s << filename << seq_number << ".txt";
    filename = s.str();
    std::cout<< filename << std::endl; // <-- Here open your file instead print the filename
}
#包括
#包括
#包括
int main(int argc,char const*argv[]
{
std::字符串文件名;
整数序号=10;
filename=“character”;
std::strings;

请注意,这需要在标准库实现中提供C++11支持。如果出现问题,请使用
fsave.open(s.str().C_str());
。总有一天,我们将能够停止添加此类限定符。有一天,很快?可能。
filename(x)
语法只能在构造字符串时使用。之后您可以使用
=
语法。如果您更改了语法,您的代码应该可以工作。感谢您的精彩解释,是的,我知道边做边测试在编码中是个坏主意,我只是尽了最大努力,直到弄不清楚为止,这就是我现在寻求帮助的原因;)