CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    9

    How to output in a .txt file?

    Hi,

    I have a programme that shows me a lot of values in the console. I would like to have the output information in a .txt file?

    Can somebody explain me in a simple way how I can do this? Which commands do I have to add exactly?

    I'd be grateful for answers!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to output in a .txt file?

    Victor Nijegorodov

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to output in a .txt file?

    If you're primarily using printf, open a file with fopen and write to it instead with fprintf().

    If you're primarily using cout, open a an ofstream and write to it instead of cout.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: How to output in a .txt file?

    Additionally,

    1) you can run from the command prompt :

    your_code.exe >output.txt

    2) If using cout, you can redirect the output by appropriately
    setting its rdbuf() to that of a file's rdbuf(). The main
    advantage in this message is that you can switch between
    outputting to the console or to the file at any time during
    the running of the code.

  5. #5
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: How to output in a .txt file?

    Some error messages may be output to the stderr stream.

    IIRC, you can redirect both stdout and stderr to file using.

    Code:
    prog.exe >2> out.txt
    or possibly

    Code:
    prog.exe 1>2> out.txt
    Nobody cares how it works as long as it works

  6. #6
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: How to output in a .txt file?

    You probably can use SetStdHandle with a handle to the .txt file opened in write mode.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured