Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Auslesen des Konsoleninhalts

Empfohlene Antworten

Veröffentlicht

Hallo an alle Programmierer,

folgendes Problem: Ich möchte den Text, der auf der Konsole ausgegeben wird, in einem char Array speichern. Hört sich einfach an, ist es aber scheinbar überhaupt nicht.

Kennt jemand von Euch vielleicht eine Möglichkeit, wie man den Konsolenpuffer auslesen könnte?

Bei MSDN gibt es ja die Funktionen: ReadConsoleOutput und WriteConsoleOutput. Auch gibt es ein Beispiel dazu, das 10 Zeilen erst ausgibt auf der Konsole und dann wieder blockweise einliest und dann wieder ab der 11. Zeile ausgibt:

- - - - - - - - - - - - -

#include <windows.h>

#include <iostream>

#include <Wincon.h>

using namespace std;

int main(void)

{

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n";

cout<<"HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH";

Sleep(3000);

HANDLE hStdout, hNewScreenBuffer;

SMALL_RECT srctReadRect;

SMALL_RECT srctWriteRect;

CHAR_INFO chiBuffer[800]; // [2][80];

COORD coordBufSize;

COORD coordBufCoord;

BOOL fSuccess;

// Get a handle to the STDOUT screen buffer to copy from and

// create a new screen buffer to copy to.

hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

hNewScreenBuffer = CreateConsoleScreenBuffer(

GENERIC_READ | // read/write access

GENERIC_WRITE,

0, // not shared

NULL, // no security attributes

CONSOLE_TEXTMODE_BUFFER, // must be TEXTMODE

NULL); // reserved; must be NULL

if (hStdout == INVALID_HANDLE_VALUE ||

hNewScreenBuffer == INVALID_HANDLE_VALUE)

{

cout<<"CreateConsoleScreenBuffer";

}

// Make the new screen buffer the active screen buffer.

if (! SetConsoleActiveScreenBuffer(hNewScreenBuffer) )

cout<<"SetConsoleActiveScreenBuffer";

// Set the source rectangle.

srctReadRect.Top = 0; // top left: row 0, col 0

srctReadRect.Left = 0;

srctReadRect.Bottom = 10; // bot. right: row 1, col 79

srctReadRect.Right = 79;

// The temporary buffer size is 2 rows x 80 columns.

coordBufSize.Y = 10;

coordBufSize.X = 80;

// The top left destination cell of the temporary buffer is

// row 0, col 0.

coordBufCoord.X = 0;

coordBufCoord.Y = 0;

// Copy the block from the screen buffer to the temp. buffer.

fSuccess = ReadConsoleOutput(

hStdout, // screen buffer to read from

chiBuffer, // buffer to copy into

coordBufSize, // col-row size of chiBuffer

coordBufCoord, // top left dest. cell in chiBuffer

&srctReadRect); // screen buffer source rectangle

if (! fSuccess)

cout<<"ReadConsoleOutput";

// Set the destination rectangle.

srctWriteRect.Top = 11; // top lt: row 10, col 0

srctWriteRect.Left = 0;

srctWriteRect.Bottom = 20; // bot. rt: row 11, col 79

srctWriteRect.Right = 79;

// Copy from the temporary buffer to the new screen buffer.

fSuccess = WriteConsoleOutput(

hNewScreenBuffer, // screen buffer to write to

chiBuffer, // buffer to copy from

coordBufSize, // col-row size of chiBuffer

coordBufCoord, // top left src cell in chiBuffer

&srctWriteRect); // dest. screen buffer rectangle

if (! fSuccess)

cout<<"WriteConsoleOutput";

Sleep(3000);

// Restore the original active screen buffer.

if (! SetConsoleActiveScreenBuffer(hStdout))

cout<<"SetConsoleActiveScreenBuffer";

// Das ist jetzt von mir. Ich wollte den Char ausgeben lassen, ging

// aber nicht:

//for(int i=0; i<800; i++)

//cout << chiBuffer.AsciiChar;

Sleep(10000);

}

- - - - - -- - - - - - - - - - -- - - -- ---- - -

Das Programm arbeitet einwandfrei, aber ich möchte auf den in den char eingelesenen Stringblock eigentlich zugreifen, um ihn dann weiter zu verarbeiten.

Bei "cout << chiBuffer.AsciiChar;" kommt folgende Fehlermeldung:

request for member `AsciiChar' in `chiBuffer', which is of non-class type `CHAR_INFO[800]'

Hier noch das struct CHAR_INFO:

typedef struct _CHAR_INFO

{ union { WCHAR UnicodeChar;

CHAR AsciiChar;

} Char; WORD Attributes;

}

CHAR_INFO, *PCHAR_INFO;

So Leute, jetzt seid Ihr mal wieder gefordert.

Wie kann man dem ganzen am besten begegnen?

Gruß

Der Wechselrichter :old

Danke für den Hinweis Klotzkopp, haut aber leider trotzdem nicht hin!

Liegt das vielleicht an der union, dass ich an diesen char nicht herankomme?

Gruß

Der Wechselrichter

Danke für den Code, hat auf Anhieb geklappt. :uli

Gruß

Wechselrichter

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.