c++ - Making a sequential list of files -
i have been stuck on problem while , can't seem find answer. i'm trying create multiple files same name different number @ end each time, have attempted @ first using
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; }
but gives me strange result letters jumbled up, i'm not sure how works know doesn't.
i've looked online , found stringstream or sstream, , tried that, keeps giving me errors too,
string filename; filename = "character"; ostringstream s; s << filename << seq_number; filename(s.str()); fsave.open(filename + ".txt"); fsave << "blabla" fsave.close(;)
but keep getting error:
no match call `(std::string) (std::basic_string, std::allocator >)'
i'm not sure how string stream works im working off of instinct, appreciate way possible, , think prefer doing without sstream, need way int , str , save filename string. unless know better way ;) guys
filename(s.str());
this wrong; not constructing new variable (filename
constructed), want here assignment.
filename = s.str();
then,
fsave.open((filename + ".txt").c_str());
(although, if using c++11, change not necessary)
still, construct whole file name stream:
ostringstream s; s<<"character"<<seq_number<<".txt"; fsave.open(s.str.c_str());
i'm not sure how string stream works im working off of instinct
this bad idea, c++ quite minefield of bizarre syntax, segfaults , undefined behavior, going instinct leads disaster.
about errors get:
fsave.open("filename" + seq_number + ".txt");
this shouldn't compile, since summing integer const char *
(thus moving "start of string"), , summing again const char *
, not allowed @ all. maybe compile if this:
fsave.open("filename" + seq_number);
but won't give required result - "filename"
pointer (not c++ string), summing integer moves pointer of given offset.
in second snippet, instead, using object (filename
) function, allowed if class overloads operator()
; thus, compiler complains such operation not allowed on object.
Comments
Post a Comment