Thread: putting output to a file

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    putting output to a file

    Hi all,

    I have aproblem with a program, and i hope someone can halp me out....
    I want to put the output, wich i see on the dos console, on a txt file.
    I know i can do that with fputs, but i think its a very long procedure to build in the fputs line after each printf line i want to see on the file.
    Isn't there some other way, to put the whole output on a txt file?
    (I also want to keep seeing it on my console too !) .
    Isn't there a command, wich copies the screen on the file?
    Can somebody help me out ?

    Thanks in advance...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    myprog.exe
    This displays on the screen

    myprog.exe > file.txt
    This stores the output in a file.

    If you want to see both at the same time, write your code better or get Unix/Linux

    myprog.exe | tee file.txt
    This stores output in file.txt AND also displays on the screen.

    >but i think its a very long procedure to build in the fputs line after each printf line
    Well it's not to hard to do this
    Code:
    int myprintf ( const char *format, ... ) {
      // some magic using vsprintf
      if ( outputfile ) {
        fputs( buff, outputfile );
      }
      fputs( buff, stdout );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    2
    Thank you for replying, but can i put in "myprog.exe > file.txt " in the program?
    I think its a command to use in the dosconsole, before the prompt....
    Can i build that also in?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sure, you implement "myprintf" as shown, and call it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM