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...
Multioperational code
-
- Member
- Posts: 524
- Joined: Sun Nov 09, 2008 2:55 am
- Location: Pennsylvania, USA
Re: Multioperational code
Code: Select all
#ifdef WIN32
system("cls");
#else
system("clear");
#endif
-
- Member
- Posts: 566
- Joined: Tue Jun 20, 2006 9:17 am
Re: Multioperational code
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
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
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
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
-
- Member
- Posts: 566
- Joined: Tue Jun 20, 2006 9:17 am
Re: Multioperational code
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
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
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.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
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Re: Multioperational code
ever heard of ncurses and escape codes?Creature wrote: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.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
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
Re: Multioperational code
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
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