Dateiausgabe als txt
-
Hallo,
ich möchte Ausgabe meiner Datei in einer Txt File speichern. Wenn ich einen String schreibe, dann klappt alles, aber ich mochte eine Tabelle ausgeben.
vielleicht kann mir jemand helfen.
#include <fstream> #include <string> #include <vector> #include <iostream> using namespace std; void funktion() { vector<double> kosten(12); for(size_t i = 0; i < kosten.size(); ++i) { kosten[i] = (150-i*i)/10.0; } // Tabelle ausgeben for(size_t i = 0; i < kosten.size(); ++i) { cout << i << ": " << kosten[i] << endl; } } int _tmain(int argc, _TCHAR* argv[]) { funktion(); fstream myFile; myFile.open("n.txt", ios::out); myFile << funktion() << '\n'; myFile.close(); char ch; cin >> ch; return 0; }danke euch!
-
// ... #include <sstream> std::string funktion() { vector<double> kosten(12); std::string tmp; for(size_t i = 0; i < kosten.size(); ++i) { kosten[i] = (150-i*i)/10.0; } for(size_t i = 0; i < kosten.size(); ++i) { std::stringstream strm; strm << i << ": " << kosten[i] << endl; tmp += strm.str(); } return tmp; } // ...
-
#include <iomanip> // std::setw #include <fstream> #include <vector> #include <cstddef> // std::size_t template <typename Container> void tableWriter(std::ostream& os, const Container& con) { std::size_t counter(0); std::size_t size(con.size()); std::size_t width(0); while(size != 0) { size /= 10; ++width; } for(typename Container::const_iterator beg(con.begin()), end(con.end()); beg != end; ++beg) os << std::setw(width) << counter++ << ": " << *beg << "\n"; } int main() { const std::vector<double>::size_type anzahl(25); std::vector<double> kosten; for(std::vector<double>::size_type i(0); i != anzahl; ++i) kosten.push_back((150 - i * i) / 10.0); std::ofstream file_out("kostentabelle.txt"); tableWriter(file_out, kosten); return 0; }
-
hallo,
ich habe beide vorschläge ausprobiert und beide funktionieren prima. danke für die Hilfe.
Grüße