Thread: Changing the path of File Descriptors.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    Changing the path of File Descriptors.

    MMkay. This is a general question regarding the first three file descriptors (stdin, stdout, stderr). They all have defaults; the keyboard, the monitor, and the monitor, respectively. How are these changed? For instance if you hooked up too monitors. How could you have your stdout be directed to one and your stderr directed to the other? What about having stdin directed to a microphone? Is this changed in code or is this a setting in the operating system that has to be changed? I don't need this for a program I was just wondering about it and figured this would be a good place to ask.

    I might also expect the answer to be that these paths can't be changed. For instance if you had a second monitor, it might be it's own separate descriptor. Where 1 is your stdout 4 might be your secondary output. I don't really know I'm just speculating. What do the descriptors coorespond with, anyway? Are they dealing with the ports on the back of your computer or what? Any light that could be shead on this subject would be wonderful.
    Last edited by SlyMaelstrom; 03-08-2006 at 07:19 PM.
    Sent from my iPadŽ

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    unfortunately, no. for one, those predefined streams are constant. you can redirect them, but not reassign them. second, the C library FILE* routines (and C++ streams) are just abstractions that ultimately map to operating system-specific calls - and I'm pretty sure that the functionality to do those sorts of things isn't built into any current implementations - you'll have to rely on API's for the particular OS you're using.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, it can be done... if you're writing the operating system and it's compilers, but that's probably not the answer you wanted...

    Since we're near the subject, I felt like writing an example as to how stream redirection can cut down on code by alot--especially when you need to write things to two files or print things to the screen and write them to a file.

    Code:
    #include <iostream>
    #include <fstream>
    
    void print(std::ostream&out,const char*data);
    
    int main()
    {
    	std::fstream file("test.dat",std::ios::out|std::ios::trunc);
    
    	print(file,"Hello World\n");
    	print(std::cout,"dlorW olleH\n");
    
    	file.close();
    	return 0;
    }
    
    void print(std::ostream&out,const char*data)
    {
    	out<<data;
    }
    of course, like this it won't be very useful, but if you rewrote the function to always output to everything you need, it just takes one function call to output to every target.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    An easier way:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) 
    {
    	fclose(stdout);
    	fopen("test.txt","w");
    	printf("Hello World\n");
    
    	return 0;
    }
    On linux this can get a lot more interesting since you can remap stdin, stdout, and stderr to anything you can open with an open() system call. This includes sockets, hard drives, the mouse, a microphone, etc.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Of course, actually saying which OS you do have would help...

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Salem
    Of course, actually saying which OS you do have would help...
    Nah, it really wouldn't. As I said, I'm not looking for that specific of an answer. If it was OS specific, then the answer "It's OS specific" would be about all I was looking for. Plus, as I said, I'm not doing this for a program, I'm doing it out of curiosity. If I'm working on Windows it doesn't mean I don't want to know how it's done on Linux.

    Anyway, for what I've seen in this post is the file descriptors can be remapped on *nix but not on Windows, yet they're constants so they can't really be changed at all. Also we all seem to be in agreement that it's OS specific because the output and input commands are mapped to system calls.

    That's not bad. It's information and that's all I was looking for. If you have any more that's wonderful, but at least we got somewhere.
    Sent from my iPadŽ

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Ok, so to be vague, the answer is somewhere between "easy" and "forget it".

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the answer has two parts: Yes, it can be done, No, you cannot do it.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Salem
    Ok, so to be vague, the answer is somewhere between "easy" and "forget it".
    So what's this supposed to say? I didn't provide enough information for someone to give a valid answer? Look at the posts above yours. They're all valid in my book. They provide good info on the topic and probably all I need to know unless someone had more input. Some people even provided code to show how one might redirect output to another source. The rest of the answers stated to pass different output to different consoles it would require OS specific API functions.

    That's all good information. It's also information that brought me to a conclusion much more concrete than "between 'easy' and 'forget it'."

    ...and major I won't even begin to critique your humor, there.
    Sent from my iPadŽ

  10. #10
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    The standard file descriptors for stdin, stdout, and stderr are a convention inherited from the earliest days of Unix.

    On a Unix like system, the only requirement on those descriptors is that they are open to appropriate serial streams when a program is started; what's done with them after that is up to the program itself.

    A shell could be written that connects descriptor 0 (stdin) to the output stream of a speach-to-text converter, descriptor 1 (stdout) to the input stream of a text-to-speach converter, and 2 (stderr) to the input stream of a console emulator that controls monitor 2, or to a file. These are, afterall, just streams, not special devices.

    A program that is has these file descriptors set up as described can then execute another program, and that program will inherit the same streams, so no redirection is required.

    On Windows and DOS, this might be more difficult, as you don't have the character stream as the basic file type as deeply embedded in the design.
    Insert obnoxious but pithy remark here

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the answer has two parts: Yes, it can be done, No, you cannot do it.
    ROFLMA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM