Thread: copy application-output into a txt-file

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

    copy application-output into a txt-file

    Hi,

    at first i want to say, that iīm a beginner in c-programming and i didnīt find any thread which helps me

    now i try to describe my problem:

    i want to save the output from an application (for example "tracert.exe" from Windows) into a txt.file. But i donīt know how i have to code it.

    i tried to use the function 'system("tracert.exe .....")' from <stdlib.h> and fill this output in an array so i can write the characters of the array in this txt.file, but it doesnīt work

    i think the cause is the "printf()" function from "tracert" - can i devert the output? if yes - how?

    can anybody help me or give me some advice?

    thx

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    tracert.exe > output.txt

    If you enter this on the command line the output (stdout) of the program tracert.exe is written to the file output.txt.

    System("tracert.exe > output");
    This should work.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    2
    thx Sargnagel for this advice. it is working now

    i think i tried already - but i got the error "to many arguments (or parameters)" - its possible that i made an other mistake - i dont know it anymore.

    i am really happy that this code is working now many many thx

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Here's another way which reads the output directly in to you're program by using _popen(). This example is running the DIR command then printing the DIR output with printf(). Then you can write to file or anything, also modify the output.
    Code:
    #include <stdio.h>
    
    void Dir(char* Path)
    {
         char   Buffer[128];
         char   CommandLine[128];
         FILE* fin;
     
         sprintf(CommandLine, "DIR %s", Path);
    
         if ((fin = _popen(CommandLine, "r")) !=  NULL) 
         {
              while( !feof( fin ) )
              {
                   if( fgets( Buffer, 128, fin ) != NULL )
                   {
                        printf("My Output>>: " );
                        printf( Buffer );
                   }
              }
              _pclose(fin);
         }
    }
    
    int main()
    {
         Dir("C:\\");
    
         return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM