2017-11-29 4 views
1

キーボード入力後に "example1ext2"というテキストが既に画面に表示されている場合、 "[1] example1 [2] example2" を置き換えることが可能かどうかは疑問でした。 システム(「CLS」)のようなもの。テキストの特定の行だけです。既にコマンドプロンプトにあるテキストを置き換える方法は? C++

int main() //just an example 
{ 
    int ans; 
    std::cout << "[1] example1 [2] example2" << std::endl; 
    std::cout << "enter a choice: "; 
    std::cin >> ans; 

    if (ans == 1) 
    { 
     std::cout << "example1extended" << std::endl; 
    } 
} 
+0

変更がない限りフレームバッファを直接、またはこれを行うことを意図したパッケージまたはユーティリティを見つけます。 – roelofs

+0

[コンソール出力の一部だけを消去する](https://stackoverflow.com/questions/15063076/clearing-only-part-of-the-console-output) – roelofs

+1

cursesライブラリを検索するhttps:// en.wikipedia.org/wiki/Curses_(programming_library) –

答えて

3

最初は:C++用の「画面」はありません。典型的には端末である「何か」に対する入力と出力のみが存在する。しかし、この端末がどのように動作するかは、C++標準の一部ではなく、移植性がないためです。したがって、異なる端末、特に異なるOSでの結果は異なります。

たとえば、次のような端末で作業している場合。 VT100のサポートでは、特殊文字を使用してカーソルを制御し、端末画面で文字を消去することができます。 https://en.wikipedia.org/wiki/VT100https://www.csie.ntu.edu.tw/~r92094/c++/VT100.html

このような種類の端末(エミュレータ)を扱う数百ものライブラリがあります。

1

これはプラットフォームに依存しない方法ですが、system('cls')と書いてあるので、私はあなたがWindows上にいると仮定します。

Windowsには、コンソールを操作するために使用される一連のユーティリティー・ファンクションであるConsole Functions APIがあります。

は、カーソルの位置を設定し、スペースで上書き:あなたはここで2つのオプションがあり

また
#include <windows.h> 
.. 
auto consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); 
COORD coordsToDelete = { row, col }; // the coordinates of what you want to delete 
// Move the current cursor position back. Writing with std::cout will 
// now print on those coordinates 
::SetConsoleCursorPosition(consoleHandle, position); 
// This will replace the character at (row, col) with space. 
// Repeat as many times as you need to clear the line 
std::cout << " "; 

を、あなたは全体のコンソールバッファを取得し、それを直接変更することができます。

#include <windows.h> 
.. 
auto consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); 
CONSOLE_SCREEN_BUFFER_INFO csbi; 
// Get a handle to the console buffer 
GetConsoleScreenBufferInfo(consoleHandle, &csbi)); 
DWORD count; 
COORD coords = { row, 0 }; 
DWORD cellCount = /* the length of your row */; 
// Write the whitespace character to the coordinates in cellCount number of cells 
// starting from coords. This effectively erases whatever has been written in those cells 
FillConsoleOutputCharacter(
     consoleHandle, 
     (TCHAR) ' ', 
     cellCount, 
     coords, 
     &count); 
関連する問題