C/C++ How to read a list of USB from C++ in ubuntu OS?

Click For Summary
To read USB connections in C++ on Ubuntu, the discussion highlights the use of the "lsusb" command, which can be executed in the terminal to list USB devices. The man page for "lsusb" indicates relevant files to access, specifically in the /proc/bus/usb directory and the /usr/share/usb.ids file. The inquiry focuses on how to replicate this functionality within a C++ program rather than using the command line. While the "system(...)" function in C can execute commands, it primarily returns an integer status code, making it less suitable for capturing output directly. The suggestion is to read the relevant files directly in C++ to obtain the necessary information, although this approach will also yield data in string format.
Nate Duong
Messages
125
Reaction score
4
I am trying to use C++ (eclipse in Ubuntu OS) to read USB connection. Usually, on the terminal window, I can use command "lsusb" to see the list. Now, I want to do the same way with C++.

can anyone help?

Thank you very much.
 
Technology news on Phys.org
The man page for lsusb provides the necessary info ie what files to read:

http://linuxcommand.org/man_pages/lsusb8.html

Notice the /proc/bus/usb directory and the file /usr/share/usb.ids
 
jedishrfu said:
The man page for lsusb provides the necessary info ie what files to read:

http://linuxcommand.org/man_pages/lsusb8.html

Notice the /proc/bus/usb directory and the file /usr/share/usb.ids

But is the question how get the list of usb connections within a C++ program instead of from the command line?

In C there is a command "system(...)" isn't there? it returns information in a string, so it is awkward to use.

Edit: I see - you're suggesting he read the files from the C++ program. That would still mean getting information in the form of strings, I suppose.
 
Stephen Tashi said:
In C there is a command "system(...)" isn't there? it returns information in a string, so it is awkward to use.
No, the system() function returns an int, typically a status code that is returned by whatever system command was called.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
353
  • · Replies 9 ·
Replies
9
Views
2K
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 14 ·
Replies
14
Views
1K
  • · Replies 27 ·
Replies
27
Views
16K
Replies
1
Views
2K
Replies
14
Views
3K