Multioperational code

Programming, for all ages and all languages.
Post Reply
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Multioperational code

Post 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...
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Multioperational code

Post by JohnnyTheDon »

Code: Select all

#ifdef WIN32
     system("cls");
#else
     system("clear");
#endif
I think WIN32 is the define you should look for.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Multioperational code

Post 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
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Re: Multioperational code

Post 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
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Multioperational code

Post 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
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Multioperational code

Post 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 :P. 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.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: Multioperational code

Post 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 :P. 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?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Multioperational code

Post 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
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Post Reply