Variable in ofstream
-
#include <iostream>
#include <fstream>
using namespace std;Hallo ich will das 4 Text Datein erstellt werden.
Test0.txt
Test1.txt
Test3.txt
Test4.txtAber ich weiss nicht mehr wie das mit der Variable geht ...
Ich meine so etwas hätte ich letzde Woche oder so noch gemacht aber ich weiss nicht mehr wie es geht
int main() { int i; for (i=0;i<3;i++) { ofstream datei("Test" <<i<< ".txt"); } return 0; }
-
-
Der Link hilft mir mal so gar nicht weiter -.-
-
#include <sstream> #include <fstream> using namespace std; int main() { for ( int i=0; i<4; i++ ) { stringstream path; path << "Test" << i << ".txt"; ofstream file( path.str().c_str() ); } }edit: Oder falls du boost draufhast:
#include <boost/lexical_cast.hpp> #include <string> #include <fstream> using namespace std; using boost::lexical_cast; int main() { for ( int i=0; i<4; i++ ) { string path = string("Test") + lexical_cast<string>(i) + ".txt"; ofstream file( path.c_str() ); } }edit2: Mensch bist du langsam, Nexus :p
-
for schleife schrieb:
Der Link hilft mir mal so gar nicht weiter -.-
Dann solltest du ihn nochmals anschauen. Dort ist genau beschrieben, wie man Text und Zahlen miteinander "vermischen" kann. Ich empfehle dir übrigens die
std::stringstream-Variante, das ist am ehesten C++.