Page 1 of 1
Multioperational code
Posted: Sun Mar 15, 2009 12:15 pm
by d4n1l0d
I want to code for windows and linux, for example, the code to clear the windows console is system("cls"); but the code to clear the linux terminal screen is system("clear");.
Since I new in C programming, I would like to now if there is any way (maybe using #ifndef ) to let the compiler recognize what system is compiling the code and then compile the correct code...
Re: Multioperational code
Posted: Sun Mar 15, 2009 12:17 pm
by JohnnyTheDon
Code: Select all
#ifdef WIN32
system("cls");
#else
system("clear");
#endif
I think WIN32 is the define you should look for.
Re: Multioperational code
Posted: Sun Mar 15, 2009 12:42 pm
by DeletedAccount
Hi,
I don't really recommend system , you will bump into problems later
For Windows use functions available in Win32 like ....
(1) GetStdHandle(STD_OUTPUT_HANDLE)
(2) GetConsoleScreenBufferInfo(...)
(3) FillConsoleOutputCharacter(... )
etc ....
For Linux and other nixes
(a) use an escape sequence
(b) use ncurses
Since you are new to C, all this might be confusing for you , so In windows you can use the conio.h header (I guess this is non standard) and call clrscr() and same clrscr() works in linux too with ncurses
.
Regards
Shrek
Re: Multioperational code
Posted: Sun Mar 15, 2009 1:31 pm
by d4n1l0d
but what's wrong with the standard C I/O functions?
Wouldn't be better to use them?
I know that scanf sucks - since I need to flush stdin before using it - but I don't see any problems doing that
Re: Multioperational code
Posted: Sun Mar 15, 2009 6:11 pm
by DeletedAccount
Hi,
System spawns a new shell process and executes your command .You may also face many security issues if you don't use system properly.But its fine for learning purposes any way
Regards
Shrek
Re: Multioperational code
Posted: Mon Mar 16, 2009 9:23 am
by Creature
d4n1l0d wrote:but what's wrong with the standard C I/O functions?
Wouldn't be better to use them?
I know that scanf sucks - since I need to flush stdin before using it - but I don't see any problems doing that
C pretty much has no functions that clear the entire console screen (as it is OS-dependent) as far as I know. You could try backspacing your way back to the first cell
. Usually you'd use system("cls") but as said before, using system is slow and might cause problems later. Win32 has some built-in stuff as said before.
Re: Multioperational code
Posted: Tue Mar 17, 2009 6:58 pm
by earlz
Creature wrote:d4n1l0d wrote:but what's wrong with the standard C I/O functions?
Wouldn't be better to use them?
I know that scanf sucks - since I need to flush stdin before using it - but I don't see any problems doing that
C pretty much has no functions that clear the entire console screen (as it is OS-dependent) as far as I know. You could try backspacing your way back to the first cell
. Usually you'd use system("cls") but as said before, using system is slow and might cause problems later. Win32 has some built-in stuff as said before.
ever heard of ncurses and escape codes?
Re: Multioperational code
Posted: Wed Mar 18, 2009 10:18 am
by Brynet-Inc
Clearing a terminal in Unix-like/POSIX systems typically uses terminfo or termcap, for example.. clear(1) is often a link to the tput(1) command. (
..Actually, the equivalent is tput clear, the tput command's default behaviour changes depending on how the executable is named.)
When using clear/tput, the terminal type is gathered from the TERM environment variable.. relevant curses functions are used to determine the appropriate method of clearing the terminal.
Hope that clears things up a bit.. there are portable ways of clearing a terminal, but it simply doesn't extend to the Windows platform. (
..even with additional support software, Cygwin and SFU have relatively poor support for it as well.)
http://www.opengroup.org/onlinepubs/969 ... /tput.html
http://en.wikipedia.org/wiki/Tput
http://en.wikipedia.org/wiki/Termcap
http://en.wikipedia.org/wiki/Terminfo
http://en.wikipedia.org/wiki/Curses