Thread: C program to record a CMD commands output and put it in a file

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    96

    C program to record a CMD commands output and put it in a file

    Hi guys, maybe I am overworking myself, I think its something simple. I need to record the output of a CMD command to a file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    // Function prototypes
    void RecordOutput();
    
    
    int main()
    {
        char educode[100];
        char CommandOutput[128];
        printf("Welcome to the network software check software!\n");
        printf("\nPlease type your license code!");
        printf("\nEducator Code: ");
        gets(educode);
    
    
        if(strcmp(educode, "5678") == 0 )
        {
            system("ping abv.bg -t");
            RecordOutput();
        }
        else
        {
            printf("\nSorry, thats not a valid Educator Code.");
        }
    
    
        return 0;
    }
    
    
    void RecordOutput()
    {
        printf("Debug: Recording started\n");
             char CommandOutput[128];
             char *f1 = "C://Users//internalhdd//Desktop//RecordingOutput.txt";
             FILE *inFile;                                             //Declare inFile
    
    
             char Byte;
             /*int  n;*/
    
    
                                        /******USER INPUT BLOCK*******/
            gets(CommandOutput);
             /*int i=0;*/
                                        /******FILE OPEN BLOCK********/
             inFile = fopen(f1,"w");
    
    
                                        /*****MAIN PROGRAM BLOCK******/
             if(inFile == NULL)                     /* check if the input file is empty */
             {
                printf("Error: Can't Open inFile\n");
             }
    
             else
             {
                      printf("File Opened, Recording\n");
                                                    /* recording cycle */
                      while(1)
                      {
                               printf(".");                /* loading symbol */
    
    
                                                         /* if the current byte is not the end of the command's output */
                               if(Byte!=EOF)
                               {                          /* Byte = function get char from "inFile" */
                                        Byte = fgetc (CommandOutput);
                               //         printf("%d",Byte); /* the new byte from the command output */
                                                        /* we put the new byte in the file */
                                        fputc(Byte, inFile);
    
    
                               }
                                                        /* if the command is over */
                               else
                               {
                                        printf("End of the recording\n");
                               }
                        return 1;
                      }
             }
    }
    Last edited by ArakelTheDragon; 07-27-2023 at 02:14 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Look up popen(). (That's defined in POSIX, so it might or might not work, or it might have limitations, in a legacy OS like Windows.)

    Also, don't use gets(). It's been removed from modern C because it cannot be used safely. Use fgets() instead (and keep in mind that it may leave a newline character at the end of the string).

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by ArakelTheDragon View Post
    Hi guys, maybe I am overworking myself, I think its something simple. I need to record the output of a CMD command to a file.
    How can this even compile, even if your compiler allows the use of gets(), that was removed from the C11 standard? I have to assume you have not attempted to compile this code!

    "Infile" seems to be an output file, not an INput file, as it is opened in write mode.

    Where do you open the "CommandOutput" file or command using _popen()?

    You need to compile your program first, fixing all the warning and errors BEFORE asking for help, or at least ask specific questions about why it will not compile. We can't even try to compile and test this program.

    This code is not even close to the point where you should ask for help.

    Turn on your warning level, and turn up the warning level to the highest level. Not sure what compiler you are using under Windows.

    If using a Microsoft compiler and not mingw64 or cygwin, then _popen() is the function to use under a Microsoft compiler. I can't speak for mingw64 or cygwin, or an other Windows compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 07:22 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. system commands. deletion of file after program terminates
    By xddxogm3 in forum C++ Programming
    Replies: 12
    Last Post: 11-24-2003, 10:41 PM
  5. System Commands Output
    By nasir_qau in forum Linux Programming
    Replies: 2
    Last Post: 03-21-2002, 03:14 PM

Tags for this Thread