Datei zeilenweise in ein string-array speichern
-
Hallo!
Ich möchte den Inhalt einer Datei zeilenweise in einem string-array speichern um dann die zweite Zeile auszugeben.
Ich habe folgenden Ansatz:
#include<iostream> #include<fstream> #include<string> // -- unwichtig begin -- #ifndef __WATCOMC__ using namespace std; #endif #ifdef __WATCOMC__ #define String string #endif #ifdef __BORLANDC__ #pragma argsused #endif //-- unwichtig end -- int main() { ifstream sdx("0192.sdx"); string line[8192]; string tmp; for (int i=0; getline(sdx, tmp);i++) line[i]=tmp; cout << line[2]; sdx.close(); return 0; }
Doch leider gibt das Programm gar nix aus.
Wio ist der Fehler?
-
Hallo,
die Datei existiert, ist nicht leer und lässt sich auf öffnen? Bei mir gehts nämlich. Überprüf das doch einfach mal...
if(!sdx){ cout << "Datei konnte nicht geöffnet werden " << endl; }
-
Datei ist da, nicht leer (~150 Zeilen) und lässt sich auch öffen.
ifstream sdx("0192.sdx"); if(!sdx) { cout << "Datei konnte nicht geöffnet werden " << endl; return 1; } string line[8192]; string tmp; for (int i=0; getline(sdx, tmp);i++) line[i]=tmp; cout << "Zeile2:" << line[2] << "Ende"; sdx.close(); return 0;
Ausgabe:
Zeile2:Ende
Hmmm...
-
Hello again,
tja, keine Ahnung, vielleicht liegts ja auch an dem Inhalt der Datei. However, ich würds sowieso ganz anders machen:
#include <iostream> #include <string> #include <vector> int main() { ifstream sdx("test.txt"); vector<string> str_vec; if(!sdx) cout << "Datei konnte nicht geöffnet werden " << endl; string temp; while(getline(sdx, temp)) { str_vec.push_back(temp); } cout << str_vec[2] << endl; }
Vielleicht hilft dir das ja irgendwie weiter. Ein vector ist sicherer in C++ und der Code ist imho auch einfacher zu verstehen.
-
CarstenJ schrieb:
vielleicht liegts ja auch an dem Inhalt der Datei.
Als Zeilenenden werden von getline 0x0d0a (Windows-like) oder 0x0a (UNIX-like) anerkannt - 0x0d (Mac-like) jedoch nicht.*
@real_beginner:
Versuch mal folgendes:for (int i=0; getline(sdx, tmp, (char)0x0d);i++) line[i]=tmp;
Vielleicht ist das ja 'ne Mac-Datei...
* Zumindest ist das bei mir so. Bei 'nem Mac-C++-Compiler wirds wohl nicht so sein...
PS: line[2] ist die dritte Zeile
-
Cool, das funktioniert! Danke!
Hab ich übrigends hierfür gebraucht:
// sdx2vdr.cpp #include<iostream> #include<fstream> #include<string> #include<vector> using namespace std; int main(int argc, char* argv[]) { ifstream sdx; ofstream vdr; vector<string> line; if(argc > 1) sdx.open(argv[1]); else sdx.open("0192.sdx"); vdr.open("channels.conf", ios::trunc); if(!sdx) { cout << "Datei " << argv[1] << " konnte nicht geöffnet werden." << endl; vdr.close(); sdx.close(); return 1; } for(string tmp; getline(sdx, tmp, (char)0x0d);) line.push_back(tmp); for(unsigned int c = 0; c < line.size(); c++) { if(line.at(c).substr(28,5) == "TMPG2" && (line.at(c).substr(102,3) == "DEU" || line.at(c).substr(102,3) == "ENG") && line.at(c).substr(111,4) == "____") { vdr << line.at(c).substr(43, 8) << line.at(c).substr(115, 12) << ":"; // Ch-Name vdr << line.at(c).substr(33, 6) << ":"; // Freq vdr << (line.at(c)[42] == '1' ? "h" : "v"); //Pol vdr << ":S19.2E:"; vdr << line.at(c).substr(69, 5) << ":"; // MSymb/s vdr << line.at(c).substr(75, 4) << ":"; // V-PID vdr << line.at(c).substr(79, 4) << ":"; // A-PID vdr << "0:"; // ??? vdr << "0:"; // Crypt-ID vdr << line.at(c).substr(87, 5) << ":"; // SID vdr << line.at(c).substr(92, 5) << ":"; // NID vdr << line.at(c).substr(97, 5) << ":"; // TID vdr << line.at(c).substr(102, 3) << ":"; // Lang vdr << "0"; // Crypt vdr << endl; } } vdr.close(); sdx.close(); return 0; }