C++ program output in neues .txt file schreiben
-
Hallo,
Ich habe ein forces_prt.txt file mit ein paar Daten drin. Dazu habe ich ein C++ Program forces.cpp das die Daten in diesem File sortieren soll.
Das Program gibt die Daten aus dem .txt file im Command Window sortiert aus.
Das passt, doch möchte ich diese Daten lieber in einem neuen output .txt file speichern, da ich sie noch weiter bearbeiten muss.
Bin leider nicht so erfahren im C++Wäre dir für eine Hilfe sehr dankbar
Grüsse
.txt file:
http://cid-daa1966be9348935.skydrive.live.com/embedicon.aspx/.Public#include <vector> #include <string> #include <iostream> #include <algorithm> #include <string.h> #include <stdlib.h> using namespace std; class myItem { public: myItem(int T, int S, int I, const string& L): Time(T), Section(S), Id(I), Line(L) {} int Time, Section, Id; string Line; bool operator < (const myItem& o) const { if (Section != o.Section) return Section < o.Section; if (Id != o.Id) return Id < o.Id; return Time < o.Time; } }; int main() { string line; vector <string> Sections, TableHeads; vector <myItem> AllData; int curTime, curSection, curId; // INPUT while (getline(cin,line).good()) { cerr << line << endl; char dummy; if (sscanf(line.c_str()," SOLUTION AT RAMP POINT %*d TIME %d",&curTime) == 1) { // A new ramp point and time, restart sections curSection = -1; } else if (line.find(':') != string::npos) { // A new heading string tablehead; getline(cin,tablehead); curSection++; if (Sections.size() <= curSection) { Sections.push_back(line); TableHeads.push_back(tablehead); } } else if (line.find_first_not_of("- \r\n\t")==string::npos) { // ignore line } else { // data line int curId = line[0] == '*'? (~0u)>>1 : atoi(line.c_str()); AllData.push_back(myItem(curTime,curSection,curId,line)); } } // SORT sort(AllData.begin(), AllData.end()); // OUTPUT curSection = -1; curId = -1; for (vector<myItem>::iterator i=AllData.begin(); i!= AllData.end(); i++) { if (i->Section != curSection) { curSection = i->Section; curId = -1; printf("\n%s\n", Sections[curSection].c_str()); } if (i->Id != curId) { curId = i->Id; printf("\nTime %s\n",TableHeads[curSection].c_str()); } printf("%02d %s\n",i->Time, i->Line.c_str()); } }
-
Huhu,
hab mir den Code nicht angeschaut, weil er falsch formatiert ist (
Tags verwenden und weil ich ihn hässlich finde :rolling_eyes: ). Du liest also aus einer .txt Daten ein und gibst sie sortiert in der Konsole aus, du willst jedoch sie in eine neue .txt reinschreiben: [cpp] #include <fstream> std::ofstream output (".../path/.txt"); //...
-
Hi,
In der Tat finde den Code auch nicht gerade attraktiv
(stammt von meinem Vorgänger
)
aber was muss ich alles ändern im Bereich Output, damit die Daten ins .txt file geschrieben werden?
Ich habe das mit "ofstream output" eingebaut, doch muss ich wahrscheinlich den ganzen //output bereich umschreiben, oder?#include <vector> #include <string> #include <iostream> #include <fstream> #include <algorithm> #include <string.h> #include <stdlib.h> using namespace std; class myItem { public: myItem(int T, int S, int I, const string& L): Time(T), Section(S), Id(I), Line(L) {} int Time, Section, Id; string Line; bool operator < (const myItem& o) const { if (Section != o.Section) return Section < o.Section; if (Id != o.Id) return Id < o.Id; return Time < o.Time; } }; int main() { string line; vector <string> Sections, TableHeads; vector <myItem> AllData; int curTime, curSection, curId; // INPUT while (getline(cin,line).good()) { cerr << line << endl; char dummy; if (sscanf(line.c_str()," SOLUTION AT RAMP POINT %*d TIME %d",&curTime) == 1) { // A new ramp point and time, restart sections curSection = -1; } else if (line.find(':') != string::npos) { // A new heading string tablehead; getline(cin,tablehead); curSection++; if (Sections.size() <= curSection) { Sections.push_back(line); TableHeads.push_back(tablehead); } } else if (line.find_first_not_of("- \r\n\t")==string::npos) { // ignore line } else { // data line int curId = line[0] == '*'? (~0u)>>1 : atoi(line.c_str()); AllData.push_back(myItem(curTime,curSection,curId,line)); } } // SORT sort(AllData.begin(), AllData.end()); // OUTPUT std::ofstream output ("//c//programme//output.txt"); curSection = -1; curId = -1; for (vector<myItem>::iterator i=AllData.begin(); i!= AllData.end(); i++) { if (i->Section != curSection) { curSection = i->Section; curId = -1; printf("\n%s\n", Sections[curSection].c_str()); } if (i->Id != curId) { curId = i->Id; printf("\nTime %s\n",TableHeads[curSection].c_str()); } printf("%02d %s\n",i->Time, i->Line.c_str()); } }
-
lG steht für liebe Grüße^^
// OUTPUT std::ofstream output ("c:\\programme\\output.txt"); curSection = -1; curId = -1; for (vector<myItem>::iterator i=AllData.begin(); i!= AllData.end(); i++) { if (i->Section != curSection) { curSection = i->Section; curId = -1; output << Sections[curSection].c_str() << std::endl; } if (i->Id != curId) { curId = i->Id; output << "\nTime " << TableHeads[curSection].c_str() << std::endl; } output << i->Time << std::endl << i->Line.c_str() << std::endl; } output.close(); }
Wenn´s nicht klappt, einfach die Fehlermeldung posten.
-
Hi Sunny31;)
Du bist der King!
Habe aber noch eine Anregung: Und zwar ist das Originalfile ziemlich gross (>400 MB) und das gibt eine unendlich lange Wurst an Daten im output file.
Da ich im Moment nicht alle Daten brauche wäre es super, wenn ich nur die
"FORCES FROM REACTIONS AT POINT TIES:" und "FORCES FROM REACTIONS AT RESTRAINED FACES :" in zwei separate output .txt files extrahieren könnte.
Eventuell mit einer if-Schlaufe nach "tablehead" suchenVielen Dank schon mal für deine Hilfe,
LG, alpenkönig
-
Hi Sunny31,
Habe mal auf eigene Faust versucht ein paar Änderungen zu implementieren.
Habe noch einen kleinen Haken drin (Zeile 46). Möchte einfach definieren, dass nicht alle Daten gelesen werden, sondern nur Daten von zwei verschiedenen Headern:#include <vector> #include <string> #include <iostream> #include <algorithm> #include <string.h> #include <stdlib.h> #include <fstream> using namespace std; class myItem { public: myItem(int T, int S, int I, const string& L): Time(T), Section(S), Id(I), Line(L) {} int Time, Section, Id; string Line; bool operator < (const myItem& o) const { if (Section != o.Section) return Section < o.Section; if (Id != o.Id) return Id < o.Id; return Time < o.Time; } }; int main() { string line; string header1 = "FORCES FROM REACTIONS AT POINT TIES:"; string header2 = "FORCES FROM REACTIONS AT RESTRAINED FACES :"; vector <string> Sections, TableHeads; vector <myItem> AllData; int curTime, curSection, curId; // INPUT while (getline(cin,line).good()) { cerr << line << endl; char dummy; if (sscanf(line.c_str()," SOLUTION AT RAMP POINT %*d TIME %d",&curTime) == 1) { // A new ramp point and time, restart sections curSection = -1; } else if (line.find(':') != string::npos) { // A new heading string tablehead; getline(cin,tablehead); curSection++; if (Sections.size() <= curSection) { Sections.push_back(line); TableHeads.push_back(tablehead); } } else if (line.find_first_not_of("- \r\n\t")==string::npos) { // ignore line } else if (tablehead==header1 || tablehead==header2) { // data line int curId = line[0] == '*'? (~0u)>>1 : atoi(line.c_str()); AllData.push_back(myItem(curTime,curSection,curId,line)); } else { // ignore line } } // SORT sort(AllData.begin(), AllData.end()); // OUTPUT std::ofstream output ("//gt//home//h113027//GT26//forces_moments//final1//output.txt"); curSection = -1; curId = -1; for (vector<myItem>::iterator i=AllData.begin(); i!= AllData.end(); i++) { if (i->Section != curSection) { curSection = i->Section; curId = -1; output << Sections[curSection].c_str() << std::endl; } if (i->Id != curId) { curId = i->Id; output << "\nTime " << TableHeads[curSection].c_str() << std::endl; } output << i->Time << std::endl << i->Line.c_str() << std::endl; } output.close(); }
Du hast mir wirklich schon gute Ratschläge gegeben, hoffe du hast hierfür auch eine passende Information bei der Hand
Grüsse