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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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