Redirecting non-standard file descriptors
On a normal executable, the system will prepare file descriptors 0, 1, and 2 for stdin, stdout and stderr. This allows for shell commands such as:
Code:
myBin < myInputFile > myOutputFile 2> myErrorLogFile Code:
myBin 3< WeirdInputFile How would you access such a thing in C++ (or C for that matter)? How can you query the OS to see if a given file descriptor (here 3) is open already when execution starts? I would prefer to work with <iostream> classes, but am willing to deal with C functions if I need to. Background: I am working on a project for school. Besides being curious about this topic, I think this would simplify my testing code. The instructor is emphasizing drivers and stubs, and stubs would be much easier if each could read from it's own stream. Something like: Code:
myBin < test42.in > test42.myout 2>test42.myerr 3< test42.firststub 4< test42.secondsstub 5< test42.thirdstub Any clues would be appreciated. (And before you say it, yes I could use normal file IO but that wouldn't satisfy my curiosity. And named pipes are cool too.) |
If there are any C++ experts out there, please jump right in, because when it comes to C++, I am but dust and ashes. But maybe I can help anyway.
This post is divided into two parts: the C part (for whose accuracy I can vouch), and the C++ part. === Part 1: the C part. In C, you can read using low-level system calls open(), close(), read(), and so on. Or you can use high-level library calls fopen(), fclose(), fgets(), and so on. (Don't ever, ever use gets().) The following program illustrates doing what you want for unit 3: Code:
#define UNIT_NUMBER 3 Code:
evans:~$ cc tuesday.c -o tuesday For C++, I googled: Code:
C++ "file descriptor" Code:
http://wwwwbs.cs.tu-berlin.de/user-taipan/kraxel/gnuinfo/iostream/Files.html |
It does help. Thank you for taking the time to respond. I'm still looking for the C++ way to do it. Now at least I know I have a way that I can imitate.
Thanks again. |
I have since edited my first response to include a possible C++ approach. We missed by minutes. Sigh.
|
Quote:
Maybe it's too late at night, and I'm not thinking clearly, but I can't get it to compile. Here's my code: Code:
#include <iostream> Code:
$ g++ fdStream.cpp For reference Code:
$ g++ -v |
I looked at more of the google results. I don't have the time to learn more about C++ at this point (I wish I did), but does this link help?
Code:
http://docs.sun.com/source/819-3704/bas_4928.htm |
Since you are using the latest compiler you will have less luck with backward compatibility when using non-standard code.
|
Hya,
May be a little bit off topic. However, Code:
$thisIsMyprogram 3< thisFileIsWhatIWantToFeedThrough3 Happy Penguins! |
Quote:
Here is what I am thinking: Code:
//old Code:
//new ta0kira |
ta0kira, let's discuss that after we've solved gd2shoe's problem. Either that, or post it in a different thread. It's amazing how easy it is for well-meaning people (including me) to hijack a thread without meaning to.
gd2shoe, how's it going? Found an answer yet? |
All times are GMT -5. The time now is 05:53 PM. |