Note that there are some explanatory texts on larger screens.

plurals
  1. POget a character at a given position in a linux console application
    primarykey
    data
    text
    <p>In the following program I'm trying to improve the function <code>GetCharAt</code> which returns a character at a given position and <code>SetCursorPosition</code> which moves the terminal cursor to a given position, in a <strong>linux</strong> C++ console application. But each function interferes with the other. for example in <code>main</code>, commenting out <code>SetCursorPosition</code> will bring back normal functionality of <code>GetCharAt</code>.</p> <pre><code>#include &lt;streambuf&gt; #include &lt;iostream&gt; using namespace std; #include &lt;stdio.h&gt; string console_string; struct capturebuf : public streambuf { streambuf* d_sbuf; public: capturebuf(): d_sbuf(cout.rdbuf()) { cout.rdbuf(this); } ~capturebuf() { cout.rdbuf(this -&gt; d_sbuf); } int overflow(int c) { if (c != char_traits&lt;char&gt;::eof()) { console_string.push_back(c); } return this -&gt; d_sbuf-&gt;sputc(c); } int sync() { return this -&gt; d_sbuf-&gt;pubsync(); } } console_string_activator; char GetCharAt(short x, short y) { if(x &lt; 1) x = 1; if(y &lt; 1) y = 1; bool falg = false; unsigned i; for(i = 0; 1 &lt; y; i++) { if(i &gt;= console_string.size()) return 0; if(console_string[i] == '\n') y--; } unsigned j; for(j = 0; console_string[i + j] != '\n' &amp;&amp; j &lt; x; j++) { if(i + j &gt;= console_string.size()) return 0; } if(i + j - 1 &lt; console_string.size()) return console_string[i + j - 1]; return 0; } void SetCursorPosition(short x,short y) { char buffer1[33] = {0}; char buffer2[33] = {0}; string a = "\e["; sprintf(buffer1,"%i",y); sprintf(buffer2,"%i",x); string xx = buffer1; string yy = buffer2; cout&lt;&lt; a + xx + ";" + yy + "f"; cout.flush(); } void SetCursorPosition2(short x, short y) { printf("\e[%i;%if",x,y); cout.flush(); } int main() { SetCursorPosition(1,1); // comment out this line for normal functionality cout &lt;&lt; "hello" "\n"; for(unsigned j = 1; j &lt;= 5; j++) { printf("%c",GetCharAt(j,1)); } cout&lt;&lt; "\n"; } </code></pre> <p>How can I change <code>SetCursorPosition</code> so that it does not interfere with <code>GetCharAt</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload