for Schleife funktioniert nicht !?



  • Hallo Leute!
    Ich möchte mittels einer Forschleife aus Verzeichnissen eine .txt Datei auslesen. Dabei ändert ich mittels for-schleife immer die target-dir:

    threads/threadC/info.dat

    Es ändert sich jeweils nur das "ThreadC", das ich mittels for-schleife neu bestimme! (z.B. threads/thread1/info.dat oder threads/thread2/info.dat)

    Mein Problem besteht darin, dass ich jeweils nur die Datei aus dem ersten Verzeichnis - sprich "threads/thread1/info.dat" auslesen kann und die schleife sich dann beendet! Warum liest er mir nur die erste Datei aus und die anderen 2 nicht ? Diese sind 100 % vorhanden und beschrieben ?!

    Quellcode:

    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <direct.h>
    #include <stdlib.h>
    #include <fstream> 
    #include <cstdlib>
    #include <strstream> 
    
    using namespace std;
    
    int main() {
    unsigned int c;
    c = 1;
    
      for(c;c<=3;c++){
    
    //c in string casten und dir von Info.dat deklarieren
    
        char buffer[10]; 
        ostrstream Str(buffer, 10); 
        Str << c << ends; 
        string sc(Str.str());    
    
         string infofiledir;
          infofiledir = "threads/thread";
          infofiledir += sc;
          infofiledir += "/info.dat";
    
    //Auslesen des Files
    
    cout<<infofiledir;
    
    cout<<endl<<endl;
    cout<<"Thread "<<c<<":"<<endl;
    
      string topic, writer, pcount, pdate; 
      unsigned int d = 1, e = 2, f = 3, g = 4 ;
    
      ifstream infofile(infofiledir.c_str()); 
    
       while(getline(infofile, topic) ){
       if (c++ == d)
        cout<<" Topic: \t \t"<<topic<<endl; 
       }
    
      unsigned int c = 1;
       ifstream infofileb(infofiledir.c_str());
       while(getline(infofileb, writer) ){
       if (c++ == e)
        cout<<" Gepostet von: \t \t "<<writer<<endl; 
       }
    
      unsigned int h = 1;
       ifstream infofilec(infofiledir.c_str());
       while(getline(infofilec, pcount) ){
       if (h++ == f)
        cout<<" Antworten gesamt: \t "<<pcount<<endl; 
       }
    
      unsigned int i = 1;
       ifstream infofiled(infofiledir.c_str());
       while(getline(infofiled, pdate) ){
       if (i++ == g)
        cout<<" erstellt am: \t \t "<<pdate<<endl; 
       } 
    
      cout<<endl;
    
      }
    
    int end;
    cin>>end;
    
    return 0;
    }
    

    Mein Compiler findet keinen Fehler ... Hoffe ich habe mich verständlich ausgedrückt und mir kann jemand helfen !? Danke!

    Gez:
    hobbyc++ler



  • Hallo

    2x

    if (c++ == d)
    

    bis bald
    akari



  • Danke! Jetzt habe ich auch verstanden warum das nicht funktioniert ^-

    MFG!
    hobbyc++ler



  • Hallo

    -nimm mal lieber die Variablendeklarationen aus der Schleife.
    -gib den Vaiablen sinnvolle Namen, und schau ob du wirklich für jeden Mist eine neue Variable brauchst.
    -solltest du auch nicht in einer Schleife c neu deklarieren und belegen

    bis bald
    akari


Anmelden zum Antworten