how to identify which OS I am using

Reply

Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: how to identify which OS I am using

 
0
  #11
Oct 2nd, 2008
Unix has various flags.
It doesn't have to do with makefiles.

Try writing instead of _UNIX:
_unix, unix, _UNIX_, UNIX... etc...

The best thing is: see what compiler are you using, and then look at its documentation, because compilers are setting those flags, and different compilers name different names (for exactly, or almost, the same thing)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: how to identify which OS I am using

 
0
  #12
Oct 2nd, 2008
I would sugguest, for the sake of portability, you never use system() calls. Instead clear the screen manually, or just print a large number of end lines.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: how to identify which OS I am using

 
0
  #13
Oct 2nd, 2008
To do the actual OS test, I would just check if c:\boot.ini exists. In the case of vista, since it uses a different booting scheme, you might want to check for another windows-specific system file, altough you would need elevated permissions to look inside the windows folder (i think). To do this, just use the fstream, open the file to be tested via ios::in, and test if it actually opened with .is_open() . If the windows files don't exist, assume linux. This is a bit of a band-aid but is a fairly surefire method of getting the OS.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to identify which OS I am using

 
0
  #14
Oct 2nd, 2008
I think we have an impressive example of a false problem.

If you want to clear console window in portable manner, use portable direct console i/o library (pdcurses, for example, or others). That's the only right answer. May be a suitable ersatz of such libraries is <conio.h> stuff implemented with minimal variations in most of C and C++ programming systems. If you need to use standard console i/o (stdio.h or iostream), never use clear screen operation in your program interface design: no such entity as a screen in C and C++ stream i/o models.

As usually you can't run executable module on different OS in native mode, so the question "is there some way to find which OS is being used at the moment of execution so the program can decide..." is a senseless sentence. The obvious answer is "your program compiled and linked for OS1 can't run on OS2 so it find nothing on OS2, moreover it can't find itself on OS2".

It's the other question: "How to write portable sources which can select proper pieces of codes for different OS by C++ conditional compilation features?". Download good portable library (or system) and look at its configuration header. Of course, it looks like Ancient Dragon's example. But it's the other story and it's not so simple in common case.
Reply With Quote Quick reply to this message  
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
  #15
Oct 2nd, 2008
If this is just some homework problem, you could try one, then the other. Only one will work:
  1. if (system( "clear" )) system( "cls" );

However, like ArkM said, better to do it the Right Way.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Re: how to identify which OS I am using

 
0
  #16
Oct 2nd, 2008
Originally Posted by ArkM View Post
Download good portable library (or system) and look at its configuration header.
Thanks for your help. Which library do you suggest? Because just printing empty lines won't really work, as this way the program contents will always apeear at the bottom of the screen and that's no good for me :/

Is there any method I can do this without using a library? Because it is for this term's project that we should do ourselves, and maybe the teacher won't find it funny for me to use libraries made by others...

Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: how to identify which OS I am using

 
0
  #17
Oct 2nd, 2008
Originally Posted by onemanclapping View Post
what do you mean by "create the makefiles for each os"? sorry, I'm a bit of a noob :/

I did:
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5.  
  6. void clearScreen()
  7. {
  8. #ifdef _WIN32
  9. system("cls");
  10. #elif defined(_UNIX)
  11. system("clear");
  12. #endif
  13. }
  14.  
  15. int main()
  16. {
  17. cout << "coco" << endl;
  18. cin.get();
  19. clearScreen();
  20. cout << "coco2" << endl;
  21. }

and the result:
  1. omc@linus-marx:~$ /home/omc/workspace/LigaSagres/Debug/LigaSagres
  2. coco
  3.  
  4. coco2
  5. omc@linus-marx:~$

so it didn't work. I guess maybe because I have not created "the makefiles for each os"
What do you mean by "it didn't work" ? I don't know what that stuff is that you posted.

"makefile for each os" means that you have to compile your program in each os -- once under *nix and again under MS-Windows. You can't compile once and run on both operating systems -- that won't work.

If you use g++ compiler then you can probably have one source file and use the same compiler on both os. You will need a version of g++ for unix and another version of g++ for MS-Windows.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: how to identify which OS I am using

 
1
  #18
Oct 2nd, 2008
for example on Linux you could compile using

g++ -D_LINUX file.cpp -o output # turns on the #define _LINUX

and on windows using

g++ -D_WIN32 file.cpp -o output #turns on #define _WIN32

So if you had a makefile on both systems, you could set your compiler flags to reflect the defines you need.
Last edited by stilllearning; Oct 2nd, 2008 at 8:26 pm.
Reply With Quote Quick reply to this message  
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 Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to identify which OS I am using

 
0
  #20
Oct 3rd, 2008
I think Duodas have posted a good example of multilevel environment recognition with subsequent macros standartisation (#define __WIN32__, for example). More complicated example from the Boost library:
http://www.boost.org/doc/libs/1_36_0...orm_config.hpp

The key point is: don't dissipate OS/compiler dependencies over all your codes. Incapsulate all this stuff in the only .h header file. Declare your own portable functions (and data structures) in this file. For example, suppose its name is omgconio.h:
  1. #ifndef OMGCONIO_H
  2. #define OMGCONIO_H
  3. ...
  4. /** Explain ClrScr functionality */
  5. void ClrScr();
  6. int Peek();
  7. bool KbHit();
  8. int GetCh();
  9. ...
  10. #endif
Use only these functions calls in your source(s).

Create omgconio.cpp file with these functions inplementation. Place all OS/compiler selecting conditional compilation directives in this file only. Add omgconio.h and omgconio.cpp files to your projects.

Now you know where is OS/compiler dependency incapsulated if you will be ready to expand or correct your platform independent direct console i/o support.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC