Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read system information in C++?

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?

like image 468
Bill the Lizard Avatar asked Dec 04 '08 18:12

Bill the Lizard


People also ask

How does system () work in C?

System() Function in C/C++It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib. h> or <cstdlib> should be included to call this function.

Is read () a system call?

In modern POSIX compliant operating systems, a program that needs to access data from a file stored in a file system uses the read system call. The file is identified by a file descriptor that is normally obtained from a previous call to open.

Which command is used to show the system information?

To display system information, use the uname command. Displays the operating system name as well as the system node name, operating system release, operating system version, hardware name, and processor type.

What is read function in C?

The read() function reads data previously written to a file. If any portion of a regular file prior to the end-of-file has not been written, read() shall return bytes with value 0.


3 Answers

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.

like image 131
Martin York Avatar answered Sep 22 '22 03:09

Martin York


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.

like image 32
Johannes Schaub - litb Avatar answered Sep 22 '22 03:09

Johannes Schaub - litb


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.

like image 42
Todd Gamblin Avatar answered Sep 24 '22 03:09

Todd Gamblin