Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the name and size of all disks whether mounted or not in c++

I need a way to get the name and size (in megabytes if possible) of all disks in C++ on Linux (Arch to be specific).

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can simply read files from /sys filesystem.

For example on my machine:

chus@kagi:~$ cat /sys/block/nvme0n1/size
2000409264

Just open the file, read the number in, multiply by 512 — and you have size in bytes. If disk is present in the system — it’s there under /sys/block.

PS: The name of disk is obviously /dev/nvme0n1.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading