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

In summary: It does not return a string. To get the output of a system command, you would need to use something like popen() or similar functions.In summary, the conversation discusses using C++ to read USB connections and obtaining the list of connections through the command "lsusb." A suggestion is made to read the necessary files from the C++ program instead of using the command line. There is a discussion about the use of the system() function and alternatives such as popen().
  • #1
Nate Duong
126
3
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
  • #2
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
 
  • #3
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.
 
  • #4
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.
 

1. How do I access the list of USB devices in C++ on Ubuntu OS?

In order to access the USB devices in C++ on Ubuntu OS, you can use the libusb library. This library provides a set of functions that allow you to interact with USB devices, including reading the list of connected devices.

2. What is the structure of the list of USB devices in C++ on Ubuntu OS?

The list of USB devices in C++ on Ubuntu OS is typically represented as an array of structures. Each structure contains information about a specific USB device, such as the device's vendor and product ID, and the device's name and serial number.

3. How do I iterate through the list of USB devices in C++ on Ubuntu OS?

To iterate through the list of USB devices in C++ on Ubuntu OS, you can use a for loop or a while loop. In each iteration, you can access the information about a specific device by using the index of the array or by using a pointer to the current structure.

4. Can I filter the list of USB devices in C++ on Ubuntu OS?

Yes, you can filter the list of USB devices in C++ on Ubuntu OS based on certain criteria, such as the device's vendor or product ID. This can be done by using the appropriate functions provided by the libusb library, such as libusb_get_device_list() and libusb_get_device_descriptor().

5. How can I handle errors when reading the list of USB devices in C++ on Ubuntu OS?

In order to handle errors when reading the list of USB devices in C++ on Ubuntu OS, you can use the error handling functions provided by the libusb library. These functions allow you to check for and handle any errors that may occur during the interaction with USB devices, ensuring that your code runs smoothly and without any unexpected crashes.

Similar threads

  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
2
Views
873
  • Programming and Computer Science
Replies
9
Views
2K
Replies
3
Views
347
  • Programming and Computer Science
Replies
15
Views
5K
  • Computing and Technology
Replies
21
Views
2K
Replies
23
Views
3K
  • Programming and Computer Science
Replies
14
Views
2K
Replies
1
Views
2K
Back
Top