unix - How to detect if a C filestream points to a file or a serial device? -


i'm working on c application evaluates data usb laser scanner, acts serial device. testing purpose, i'm allowing test data read file, because not convenient have scanner connected.

i open file/device this:

file *fp = fopen(argv[1], "a+b"); 

and depending on whether want read file or device, pass file path or /dev/cu.usbmodemfd121 (i'm on mac).

this works fine long i've initialized laser scanner, i'd rather have application that. in order that, though, must first figure out if i'm reading file or device. how can that, given file * returned fopen?

i've tried use fseek(fp, 1, seek_end) expected fail scanner, since it's stream doesn't have "end", reasons fseek not fail..

you file descriptor using fileno , fstat on it. struct stat populates contains thinks st_mode shows type of fd. guessing non-file device s_ischr true or @ least s_isreg false.


if have control on it, don't fopen @ all. use open directly file descriptor , use fdopen if want c streams.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -