How do I read system information in C++?

10,370

Solution 1

If you are using *nix commands via system.

Then do man scroll to the bottom of the man page and it will usually show you what relevant C system calls are related.

Example:  man uname:
SEE ALSO
       uname(2), getdomainname(2), gethostname(2)


Explanation of numbers:

(1): User UNIX Command
(2): Unix and C system calls
(3): C Library routines
(4): Special file names
(5): File formats
(6): 
(7):
(8): System admin commands

So if you are using system("uname"). From the man page you can see that there is also a uname C system call (uname(2)). So you can now do a 'man 2 uname' to get information about how to use the C system call uname.

Solution 2

There is nothing in the C++ Standard library for these purposes. The library you could use is libhal, which abstracts the view of programs to the hardware, collecting various informations from /proc, /sys and others. HAL, scroll down, there seems to be an unofficial C++ binding available too (haven't tested it though, while libhal works also fine for C++ programs). Use the command lshal to display all device informations available to HAL.

Solution 3

If you don't want to use HAL as litb suggests, you can read things straight out of the /proc filesystem, provided it's there on your system. This isn't the most platform-independent way of doing things, and in many cases you'll need to do a little parsing to pick apart the files.

I think HAL abstracts a lot of these details for you, but just know that you can read it straight from /proc if using a library isn't an option.

Solution 4

System information is by definition not portable, so there is no standard solution. Your best bet is using a library that does most of the work for you. One such cross platform library (unlike hal, which is currently Linux specific) is SIGAR API, which is open source BTW. I've used it in a C++ project without much trouble (the installation is a bit non-standard but can be figured out easily)

Share:
10,370
Bill the Lizard
Author by

Bill the Lizard

Python, Java, Android, etc. developer living in Charlotte, NC. I am the author of @BountyBot, a Twitter bot that posts new and interesting bounty questions from Stack Overflow. You can view the source on GitHub. My Android apps on Google Play: Three of a Kind - A fun, fast-paced strategy card game. Match any three symbols to win. Sketchboard - Sketch drawings on your mobile device. Serpent - An Android version of the classic mobile game Snake. Math Blitz! - A fast-paced flashcard game to help students practice their arithmetic skills. Linear Interpolator - Fill in the gaps in your data using linear interpolation. Kitchen Calculator - A simple unit converter for common units used in cooking and homebrewing. If you use any of these apps, please leave me a rating and any feedback for improvement.

Updated on June 16, 2022

Comments

  • Bill the Lizard
    Bill the Lizard over 1 year

    I'm trying to get information like OS version, hard disk space, disk space available, and installed RAM on a Linux system in C++. I know I can use system() to run different Linux commands and capture their output (which is what I'm currently doing) but I was wondering if there's a better way? Is there something in the C++ standard library that I can use to get information from the operating system?