Strange output behaviour when using system(“/bin/stty raw”)
In my program, a 5x20 field of points is displayed. A player which has the symbol "#" is also on the field. You can move the player around with w, a, s and d. If I use buffered input everything is fine. But the user dont want hit enter every time. So I used the command
system("/bin/stty raw")
The problem now is that the output behavior is very strange. The lines of the field are not displayed properly now. I wasted much time by trying to find the error by myself. Also Google seems not to have an appropriate explanation to this behavior.
Can you tell me why the program behaves that strange, and how to fix that?
#include <stdio.h>
#define FIELD_VERTIKAL 5
#define FIELD_HORIZONTAL 20
#define PLAYER_SYMBOL '#'
void print_field(char field[FIELD_VERTIKAL][FIELD_HORIZONTAL])
{
// reset display
for (int i = 0; i < 20; i++)
putchar('n');
// display field
for (int i = 0; i < FIELD_VERTIKAL; i++)
{
for (int j = 0; j < FIELD_HORIZONTAL; j++)
putchar(field[i][j]);
putchar('n');
}
}
void draw_field(char field[FIELD_VERTIKAL][FIELD_HORIZONTAL],
int pos_vertikal, int pos_horizontal)
{
for (int i = 0; i < FIELD_VERTIKAL; i++)
for (int j = 0; j < FIELD_HORIZONTAL; j++)
field[i][j] = '.';
field[pos_vertikal][pos_horizontal] = '#';
}
int main()
{
// create field and initialize it
char field[FIELD_VERTIKAL][FIELD_HORIZONTAL];
// position of player
int pos_vertikal = 0;
int pos_horizontal = 0;
// initialize field
draw_field(field, pos_vertikal, pos_horizontal);
system("/bin/stty raw");
print_field(field);
char input;
while ((input = getchar()) != EOF)
{
// move player
if (input == 'w')
{
pos_vertikal--;
}
else if (input == 'a')
{
pos_horizontal--;
}
else if (input == 's')
{
pos_vertikal++;
}
else if (input == 'd')
{
pos_horizontal++;
}
// place player and display field
draw_field(field, pos_vertikal, pos_horizontal);
print_field(field);
}
}
c
add a comment |
In my program, a 5x20 field of points is displayed. A player which has the symbol "#" is also on the field. You can move the player around with w, a, s and d. If I use buffered input everything is fine. But the user dont want hit enter every time. So I used the command
system("/bin/stty raw")
The problem now is that the output behavior is very strange. The lines of the field are not displayed properly now. I wasted much time by trying to find the error by myself. Also Google seems not to have an appropriate explanation to this behavior.
Can you tell me why the program behaves that strange, and how to fix that?
#include <stdio.h>
#define FIELD_VERTIKAL 5
#define FIELD_HORIZONTAL 20
#define PLAYER_SYMBOL '#'
void print_field(char field[FIELD_VERTIKAL][FIELD_HORIZONTAL])
{
// reset display
for (int i = 0; i < 20; i++)
putchar('n');
// display field
for (int i = 0; i < FIELD_VERTIKAL; i++)
{
for (int j = 0; j < FIELD_HORIZONTAL; j++)
putchar(field[i][j]);
putchar('n');
}
}
void draw_field(char field[FIELD_VERTIKAL][FIELD_HORIZONTAL],
int pos_vertikal, int pos_horizontal)
{
for (int i = 0; i < FIELD_VERTIKAL; i++)
for (int j = 0; j < FIELD_HORIZONTAL; j++)
field[i][j] = '.';
field[pos_vertikal][pos_horizontal] = '#';
}
int main()
{
// create field and initialize it
char field[FIELD_VERTIKAL][FIELD_HORIZONTAL];
// position of player
int pos_vertikal = 0;
int pos_horizontal = 0;
// initialize field
draw_field(field, pos_vertikal, pos_horizontal);
system("/bin/stty raw");
print_field(field);
char input;
while ((input = getchar()) != EOF)
{
// move player
if (input == 'w')
{
pos_vertikal--;
}
else if (input == 'a')
{
pos_horizontal--;
}
else if (input == 's')
{
pos_vertikal++;
}
else if (input == 'd')
{
pos_horizontal++;
}
// place player and display field
draw_field(field, pos_vertikal, pos_horizontal);
print_field(field);
}
}
c
add a comment |
In my program, a 5x20 field of points is displayed. A player which has the symbol "#" is also on the field. You can move the player around with w, a, s and d. If I use buffered input everything is fine. But the user dont want hit enter every time. So I used the command
system("/bin/stty raw")
The problem now is that the output behavior is very strange. The lines of the field are not displayed properly now. I wasted much time by trying to find the error by myself. Also Google seems not to have an appropriate explanation to this behavior.
Can you tell me why the program behaves that strange, and how to fix that?
#include <stdio.h>
#define FIELD_VERTIKAL 5
#define FIELD_HORIZONTAL 20
#define PLAYER_SYMBOL '#'
void print_field(char field[FIELD_VERTIKAL][FIELD_HORIZONTAL])
{
// reset display
for (int i = 0; i < 20; i++)
putchar('n');
// display field
for (int i = 0; i < FIELD_VERTIKAL; i++)
{
for (int j = 0; j < FIELD_HORIZONTAL; j++)
putchar(field[i][j]);
putchar('n');
}
}
void draw_field(char field[FIELD_VERTIKAL][FIELD_HORIZONTAL],
int pos_vertikal, int pos_horizontal)
{
for (int i = 0; i < FIELD_VERTIKAL; i++)
for (int j = 0; j < FIELD_HORIZONTAL; j++)
field[i][j] = '.';
field[pos_vertikal][pos_horizontal] = '#';
}
int main()
{
// create field and initialize it
char field[FIELD_VERTIKAL][FIELD_HORIZONTAL];
// position of player
int pos_vertikal = 0;
int pos_horizontal = 0;
// initialize field
draw_field(field, pos_vertikal, pos_horizontal);
system("/bin/stty raw");
print_field(field);
char input;
while ((input = getchar()) != EOF)
{
// move player
if (input == 'w')
{
pos_vertikal--;
}
else if (input == 'a')
{
pos_horizontal--;
}
else if (input == 's')
{
pos_vertikal++;
}
else if (input == 'd')
{
pos_horizontal++;
}
// place player and display field
draw_field(field, pos_vertikal, pos_horizontal);
print_field(field);
}
}
c
In my program, a 5x20 field of points is displayed. A player which has the symbol "#" is also on the field. You can move the player around with w, a, s and d. If I use buffered input everything is fine. But the user dont want hit enter every time. So I used the command
system("/bin/stty raw")
The problem now is that the output behavior is very strange. The lines of the field are not displayed properly now. I wasted much time by trying to find the error by myself. Also Google seems not to have an appropriate explanation to this behavior.
Can you tell me why the program behaves that strange, and how to fix that?
#include <stdio.h>
#define FIELD_VERTIKAL 5
#define FIELD_HORIZONTAL 20
#define PLAYER_SYMBOL '#'
void print_field(char field[FIELD_VERTIKAL][FIELD_HORIZONTAL])
{
// reset display
for (int i = 0; i < 20; i++)
putchar('n');
// display field
for (int i = 0; i < FIELD_VERTIKAL; i++)
{
for (int j = 0; j < FIELD_HORIZONTAL; j++)
putchar(field[i][j]);
putchar('n');
}
}
void draw_field(char field[FIELD_VERTIKAL][FIELD_HORIZONTAL],
int pos_vertikal, int pos_horizontal)
{
for (int i = 0; i < FIELD_VERTIKAL; i++)
for (int j = 0; j < FIELD_HORIZONTAL; j++)
field[i][j] = '.';
field[pos_vertikal][pos_horizontal] = '#';
}
int main()
{
// create field and initialize it
char field[FIELD_VERTIKAL][FIELD_HORIZONTAL];
// position of player
int pos_vertikal = 0;
int pos_horizontal = 0;
// initialize field
draw_field(field, pos_vertikal, pos_horizontal);
system("/bin/stty raw");
print_field(field);
char input;
while ((input = getchar()) != EOF)
{
// move player
if (input == 'w')
{
pos_vertikal--;
}
else if (input == 'a')
{
pos_horizontal--;
}
else if (input == 's')
{
pos_vertikal++;
}
else if (input == 'd')
{
pos_horizontal++;
}
// place player and display field
draw_field(field, pos_vertikal, pos_horizontal);
print_field(field);
}
}
c
c
edited Jan 2 at 11:26
Vengeancos
asked Jan 2 at 7:51
VengeancosVengeancos
124
124
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Terminals were very complex devices (and today's terminal emulators are trying to mimic them, something like the VT100; of course these emulators are operating-system specific). Read the TTY demystified page and termios(3). Be also aware of ANSI escape codes.
And stty raw
is not doing what you dream of (it is related to the line discipline). See also stty(1) and pty(7).
If you want to write a terminal text oriented application, use some library such as ncurses. So throw your code away, spend a few days reading documentation, and write your code again from scratch.
Maybe you could consider making instead your game some graphical application, e.g. by using some toolkit such as GTK or libSDL.
PS. You don't mention it, but I guess you are using some Linux system.
Yes, I use Lubuntu 16.04 (32 Bit).
– Vengeancos
Jan 2 at 17:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002944%2fstrange-output-behaviour-when-using-system-bin-stty-raw%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Terminals were very complex devices (and today's terminal emulators are trying to mimic them, something like the VT100; of course these emulators are operating-system specific). Read the TTY demystified page and termios(3). Be also aware of ANSI escape codes.
And stty raw
is not doing what you dream of (it is related to the line discipline). See also stty(1) and pty(7).
If you want to write a terminal text oriented application, use some library such as ncurses. So throw your code away, spend a few days reading documentation, and write your code again from scratch.
Maybe you could consider making instead your game some graphical application, e.g. by using some toolkit such as GTK or libSDL.
PS. You don't mention it, but I guess you are using some Linux system.
Yes, I use Lubuntu 16.04 (32 Bit).
– Vengeancos
Jan 2 at 17:18
add a comment |
Terminals were very complex devices (and today's terminal emulators are trying to mimic them, something like the VT100; of course these emulators are operating-system specific). Read the TTY demystified page and termios(3). Be also aware of ANSI escape codes.
And stty raw
is not doing what you dream of (it is related to the line discipline). See also stty(1) and pty(7).
If you want to write a terminal text oriented application, use some library such as ncurses. So throw your code away, spend a few days reading documentation, and write your code again from scratch.
Maybe you could consider making instead your game some graphical application, e.g. by using some toolkit such as GTK or libSDL.
PS. You don't mention it, but I guess you are using some Linux system.
Yes, I use Lubuntu 16.04 (32 Bit).
– Vengeancos
Jan 2 at 17:18
add a comment |
Terminals were very complex devices (and today's terminal emulators are trying to mimic them, something like the VT100; of course these emulators are operating-system specific). Read the TTY demystified page and termios(3). Be also aware of ANSI escape codes.
And stty raw
is not doing what you dream of (it is related to the line discipline). See also stty(1) and pty(7).
If you want to write a terminal text oriented application, use some library such as ncurses. So throw your code away, spend a few days reading documentation, and write your code again from scratch.
Maybe you could consider making instead your game some graphical application, e.g. by using some toolkit such as GTK or libSDL.
PS. You don't mention it, but I guess you are using some Linux system.
Terminals were very complex devices (and today's terminal emulators are trying to mimic them, something like the VT100; of course these emulators are operating-system specific). Read the TTY demystified page and termios(3). Be also aware of ANSI escape codes.
And stty raw
is not doing what you dream of (it is related to the line discipline). See also stty(1) and pty(7).
If you want to write a terminal text oriented application, use some library such as ncurses. So throw your code away, spend a few days reading documentation, and write your code again from scratch.
Maybe you could consider making instead your game some graphical application, e.g. by using some toolkit such as GTK or libSDL.
PS. You don't mention it, but I guess you are using some Linux system.
edited Jan 2 at 8:11
answered Jan 2 at 7:59


Basile StarynkevitchBasile Starynkevitch
179k13173374
179k13173374
Yes, I use Lubuntu 16.04 (32 Bit).
– Vengeancos
Jan 2 at 17:18
add a comment |
Yes, I use Lubuntu 16.04 (32 Bit).
– Vengeancos
Jan 2 at 17:18
Yes, I use Lubuntu 16.04 (32 Bit).
– Vengeancos
Jan 2 at 17:18
Yes, I use Lubuntu 16.04 (32 Bit).
– Vengeancos
Jan 2 at 17:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002944%2fstrange-output-behaviour-when-using-system-bin-stty-raw%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown