dup2 equivalant on windows?

This is a discussion on dup2 equivalant on windows? within the C Programming forums, part of the General Programming Boards category; I'm writing a program that calculates prime numbers and I would like to put the output to a file if ...

  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
    22,127
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,127
    I would make that stdout instead of stdin.
    Heheh, yeah.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    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, 03:16 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 06:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 03:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 01:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 06:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21