Datei mehrfach einlesen



  • Hallo an Alle,

    ich möchte gern eine Text-Datei mehrfach einlesen.
    Habe mir gedacht, dass dies mit einer einfachen Schleife zu machen ist.Leider funktioniert es nicht so wie ich es mir dachte.
    Kann mir jemand einen Tipp geben?

    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    #include <string>

    using namespace std;

    int main()
    {

    ifstream x;
    string pfad="c:\\test.txt";
    string inhalt;

    for(int loop=0;loop<=10;++loop)
    {
    x.open(pfad.c_str(),ios::in);
    while (!x.eof())
    {
    x >>inhalt;
    cout << inhalt<<endl;
    }
    x.close();
    }

    return 0;
    }



  • #include <iostream> 
    #include <fstream> 
    #include <string> 
    
    using namespace std; 
    
    int main() 
    { 
    
    ifstream x; 
    string pfad="test.txt"; 
    string inhalt; 
    
    for(int loop=0;loop<=10;++loop) 
    { 
    
    	x.open(pfad.c_str(),ios::in); 
    	while (!x.eof()) 
    	{ 
    		x    >> inhalt;
    		cout << inhalt << endl; 
    	}
    	cout << "Durchlauf Nr: " <<  loop << "\n";
    
    	x.clear();
    	x.close();
    
    } 
    
    return 0; 
    }
    

    So klappt es bei mir habe das "x.clear()" eingefügt.



  • Ansonsten einfach das zugriffsobjekt lokaler halten, dann klappt's auch prima:

    #include <iostream> 
    #include <fstream> 
    #include <string> 
    
    using namespace std; 
    
    int main() 
    { 
    
      string pfad="test.txt"; 
      string inhalt; 
    
      for(int loop=0;loop<=10;++loop) 
      { 
        ifstream x(pfad.c_str(), ios::in); 
        while (!x.eof()) 
        { 
            x    >> inhalt;
            cout << inhalt << endl; 
        }
        cout << "Durchlauf Nr: " <<  loop << "\n";
      } 
    }
    

    MfG Jester



  • Danke für Eure schnelle Antwort !!
    Eine Zeile mit großer Wirkung 😃 .
    Jetzt komme ich entlich weiter ....!

    Tschüß mjac


Anmelden zum Antworten