Thread: dup2 equivalant on windows?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    6

    dup2 equivalant on windows?

    I'm writing a program that calculates prime numbers and I would like to put the output to a file if the user gave a filename for an argument.

    Unfortunately I have to use windows and not linux, so the easiest option of
    Code:
    if(dup2(fileno(stdout), open(argv[1], O_WRONLY | O_CREAT | O_EXCL))<0)
        perror("Error");
    }
    and then printing to stdout all the time wont work. Is there another option I can use to do this?

    I would like to avoid ifs in the actual section where I would print the primes in order to prevent the speed loss.

    PS: If I have made a mistake in that code snippet and it would actually all work... I'm an idiot, sorry :P

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why not just use fprintf() with stdin if no filename is provided, or with the appropriate file handle if it is? Alternatively, just printf() and have the user redirect the output if necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    .... stdin ...
    I would make that stdout instead of stdin.

    The way I've done this in the past is to open a file (FILE *f = fopen(...)) if available, or set the file-pointer to stdout if there is no file to open.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would make that stdout instead of stdin.
    Heheh, yeah.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    I suppose I could leave it and have the user redirect... but I would prefer the other way (out of simple curiosity at this point).

    Edit:
    The way I've done this in the past is to open a file (FILE *f = fopen(...)) if available, or set the file-pointer to stdout if there is no file to open.
    I'll look into this and see how it goes.
    Last edited by dasbush; 01-29-2008 at 10:57 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    so something like:
    Code:
       FILE *outfile;
       if (argc == 2) {
          f = fopen(argv[1], "w");
          // Need error checking here! 
       else
          f = stdout;
    
       ... 
       fprintf(f, "%d" ...);
       ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    Works like a charm, thanks a bunch!

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you actually want to redirect stdout there are more portable ways to do that than calling dup2().

    Code:
    freopen(argv[1], "w", stdout);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 98/2000 programming in Windows XP
    By Bill83 in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:16 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM