View Single Post
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: how to identify which OS I am using

 
0
  #19
Oct 3rd, 2008
Sigh. You know, you really shouldn't be dinking with the screen at all. But if all you want is something to clear the screen in Win32 and POSIX, here are a couple of functions. Your teacher will know you didn't write them yourself!

  1. // clearscreen.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. #if defined(__WIN32__) || defined(__WIN32) || defined(_WIN32) || defined(WIN32) || defined(__TOS_WIN__) || defined(__WINDOWS__)
  7. #ifndef __WIN32__
  8. #define __WIN32__
  9. #endif
  10. #include <windows.h>
  11. #else
  12. #include <unistd.h>
  13. #ifdef _POSIX_VERSION
  14. #ifndef __UNIX__
  15. #define __UNIX__
  16. #endif
  17. #include <term.h>
  18. #endif
  19. #endif
  20.  
  21. #if !defined(__WIN32__) && !defined(__UNIX__)
  22. #warning Works best on a Windows or POSIX platform.
  23. #endif
  24.  
  25. void clearscreen()
  26. {
  27. #if defined(__WIN32__)
  28.  
  29. HANDLE hStdOut;
  30. CONSOLE_SCREEN_BUFFER_INFO csbi;
  31. DWORD count;
  32. DWORD cellCount;
  33. COORD homeCoords = { 0, 0 };
  34.  
  35. hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  36. if (hStdOut == INVALID_HANDLE_VALUE) return;
  37.  
  38. /* Get the number of cells in the current buffer */
  39. if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  40. cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  41.  
  42. /* Fill the entire buffer with spaces */
  43. if (!FillConsoleOutputCharacter(
  44. hStdOut,
  45. (TCHAR) ' ',
  46. cellCount,
  47. homeCoords,
  48. &count
  49. )) return;
  50.  
  51. /* Fill the entire buffer with the current colors and attributes */
  52. if (!FillConsoleOutputAttribute(
  53. hStdOut,
  54. csbi.wAttributes,
  55. cellCount,
  56. homeCoords,
  57. &count
  58. )) return;
  59.  
  60. /* Move the cursor home */
  61. SetConsoleCursorPosition( hStdOut, homeCoords );
  62.  
  63. #elif defined(__UNIX__)
  64.  
  65. static char* console_clearscreen = 0;
  66. if (!console_clearscreen)
  67. {
  68. int result;
  69. setupterm( NULL, STDOUT_FILENO, &result );
  70. if (result > 0)
  71. console_clearscreen = tigetstr( "clear" );
  72. }
  73. if (console_clearscreen)
  74. putp( console_clearscreen );
  75.  
  76. #else
  77. std::cout << std::string( 100, '\n' );
  78. #endif
  79. }
  80.  
  81. // end clearscreen.cpp
  1. // clearscreen.hpp
  2.  
  3. #ifndef CLEARSCREEN_HPP
  4. #define CLEARSCREEN_HPP
  5.  
  6. void clearscreen();
  7.  
  8. #endif
  9.  
  10. // end clearscreen.hpp
Enjoy.
Reply With Quote