Click to See Complete Forum and Search --> : Accessing usb using c++
Pitufo
October 13th, 2008, 04:52 PM
Lets say that there is a device connected to one of the usb ports in my computer. That device is sending numbers to my computer. I want to create a program that can access the usb port and collect the data from it. I have this code and got stuck here...can someone please help me?
#include <stdio.h>
#include <iostream>
using namespace std;
int main ()
{
FILE * pFile;
pFile = fopen("f:\\[/b]", "r");
system("pause");
fclose(pFile);
return 0;
}
kirants
October 13th, 2008, 05:38 PM
USB devices can be seen by the system as different classes ( serial port, file system, modem class and such ) per my limited knowledge on that. What does the device you are concerned about appearing as ?
olivthill
October 14th, 2008, 03:29 AM
I am not sure if [/b] is correct in pFile = fopen("f:\\[/b]", "r");. Maybe it is a BBcode for bold. I would try to replace that [/b] with a filename. But maybe I am wrong. It is just a little doubt.
If the numbers are not sent as Ascii characters, then the file should be opened in binary mode with "rb" instead of being opened in text mode with "r".
The returned value, pFile, should be checked. If it is null, then there is a problem and the program should not try to read data.
Use fgetc(), or fread(), to get bytes, or fgets() to get a line.
Nitroglycerine
October 15th, 2008, 03:32 PM
Get the device name (c,d,e...)
get the file name
c://file.ext
and read it :D
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.