[solved] fstream schreibt nicht



  • Hi,
    ich hab hier eine Methode, die eine Datei oeffnen soll, eine Zeile hineinschreiben soll und dannach die Datei wieder schliesst. Das Problem ist, dass das genau ein mal funktioniert, und zwar nur dann, wenn die Datei neu angelegt wurde.

    void logLine(string line,string channel) {
    	cout << "logLine ( " << line << ", " << channel << " )" << endl;
      bool all = false; // whether we should log to all channels
      string file,timestamp; // some strings
      struct tm *ptr; // pointer
      time_t tm; // time holder
      char hold[10]; // holder
      fstream log; // filestream
    
      // check if we should be logging to all channels, private messages or one channel
      if (channel == "") { 
        // we got passed a null, so log to all channels
        all = true;
      } else if ((channel != LOG_PRIVMSG) && (!isIn(channel))) {
        // we got passed a channel we shouldnt log, so dont ;)
    	cout << "else if ((channel != LOG_PRIVMSG) && (!isIn(channel)))" << endl;
        return;
      }
    
      // loop through the channels
      for (int i = 0;i < MAX_CHANNELS;i++) {
    
        // check what we should be logging to
        if (all) { file = LOG_CHANNELS[i]; }
        else if (channel == LOG_PRIVMSG) { file = string(ME.nick); }
        else { file = channel; }
    
        // check if we have a valid looking file to log to
        if (file == "") { cout << "file == \"\"" << endl; break; }
    
        // if we have a filename with a # at the front, strip it off
        if (file.find("#",0) == 0) { file.erase(0,1); }
    
        // lowercase the filename
        transform (file.begin(),file.end(), file.begin(), static_cast<int (*)(int)>(tolower));
    
        // get the current time
        tm = time(NULL);
        // get the local time
        ptr = localtime(&tm);
        // get a nicely formatted time
        strftime(hold ,10 , "[%R] ",ptr);
        // save it as timestamp
        timestamp = hold;
        // get a nicely formated date
        strftime(hold ,10 , "%Y%m%d",ptr);
        // build the filename
        file = LOG_DIR + file + LOG_MID + hold;
    
        // check if the filename exists
        if (fileExists(file)) {
          // file exists, append
          log.open(file.c_str(),ios::app);
          cout << "ios:app" << endl;
        } else {
          // new file, open at the beginning
          log.open(file.c_str(),ios::out);
          cout << "ios::out" << endl;
        }
        // log the entry to the open file
        log << timestamp << line << endl;  
        cout << timestamp << "||" << line << endl;
        // close the file
        log.close();
        // if we are only doing 1 channel, break now
        if (!all) { break; }
      } // end of for looping through channels
    }
    

    Die Funktion habe ich nicht selber geschrieben, ich stelle die Datei gerade auf eine neue Version des Hauptprogramms um. Die Ausgabe sagt mir dass eigentlich alles funktioniert, existiert die Datei, wird "ios::app" ausgegeben, existiert sie nciht, wird "ios::out" ausgegeben. in timestamp und line (direkt nach dem schreiben in den stream) stehen die korrekten Strings drin. Ich kann keinen Fehler erkennen, die Datei wird nur von dieser Funktion benutzt.
    An sich wurde diese Funktion nicht veraendert, mit der alten Version des Programms hat sie offenbar funktioniert.
    C167



  • also beim ersten durchgehen ist mir folgendes aufgefallen:

    C167 schrieb:

    dass das genau ein mal funktioniert, und zwar nur dann, wenn die Datei neu angelegt wurde

    Das könnte daran liegen, dass hier:

    C167 schrieb:

    log.open(file.c_str(),ios::app);
    

    auch noch ein ios::out stehen muss, also

    log.open(file.c_str(), ios::out|ios::app);
    

    ( ursprünglicher Code funktioniert also nur einmal, da beim ersten Mal nicht diese Funktion aufgerufen wird, sondern die darunter stehende( zumindest gehe ich davon aus 🙂 ), welche ja "ios::out" ausführt )
    Außerdem könnte man versuchen mit "log.clear()" die Objektinternen Statusbits zu löschen, ob das hilft, ist aber nicht gewiss 🙂



  • vielen Dank, genau das war es. Ich hab ios::out dazugehaengt und schon gings los!
    mfg
    C167


Anmelden zum Antworten