Home > Enterprise >  Print CMD output to .txt with C
Print CMD output to .txt with C

Time:12-01

does anyone have an idea how to save a CMD output to a .txt with C? I would like to do a ping and tracert and then ask if the result should be saved. Should it be saved, the result should be saved in a .txt.

My code is like this:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main ()
{

    char Testprint1[100],Testprint2[100];

    sprintf(Testprint2, "ping 127.0.0.1");
    system(Testprint2);

    sprintf(Testprint2, "tracert 127.0.0.1");
    system(Testprint2);

    printf("\nDo you want to save the output? (y)Yes / (n)No: ");

    if (Answer=='j')
    {
    FILE *Test;
    Test = fopen("Test_Log.txt", "w");
    fprintf(Test, "Ping:\n%s\n\nTracert:\n%s\n",Testprint1,Testprint2);

        if(Pinglog == NULL) 
        {
        printf("Log could not be saved.\n");
            system("\n\npause\n");
        }
        else
        {
            printf("Log has been saved.");
            fclose(Pinglog);
            system("cls");
        }
    }

    else if(Answer=='n')
    {
        system("cls");
        system("\n\npause\n");
    }
}


The txt includes:

Ping: ping 127.0.0.1

Tracert: tracert 127.0.0.1

It is plausible for me that only this comes out as a result, but I have no idea how I can change that and how I can save the CMD output e.g. in a variable and then save it in the .txt.

CodePudding user response:

Your code is pretty unconventional, which should be a flag that the approach tends in a messy direction. You're using C for things that are normally scripted. I'm going to focus on the use of ping.exe.

The conventional approach in C would be to use APIs to accomplish your tasks (e.g. instead of ping.exe, call IcmpSendEcho).

Your code, on the other hand, is launching a series of external processes to do your tasks, and attempting to orchestrate them. That's something scripting languages are great at, and C is rather bad at.

While it's simple to just invoke ping.exe, the downside is you only get the control that ping.exe grants you. If you on the other hand use IcmpSendEcho, you have full control over the behavior and output.

It's possible however. ping.exe (et al) output to stdout, and scripting languages (.cmd, .sh) have natural methods of redirecting stdout. By default stdout goes to the console/shell window. You can redirect stdout by using > (e.g. ping >output.txt). You can also redirect stdout in your own application, however it's not as trivial as calling system().

At very least you will need to use CreateProcess.

There are many related questions on SO, like How do I redirect output to a file with CreateProcess?

CodePudding user response:

A simple example that reads the command ping and saves it to a buffer

#include <stdio.h>

#define BUFSIZE 1000

int main()
{
    char buf[BUFSIZE] = {0};

    FILE *p = _popen("ping google.com /n 1", "r");
    if (p == NULL) {
        puts("popen failed");
        return 1;
    }

    fread(buf, BUFSIZE - 1, 1, p);
    printf("%s", buf);

    _pclose(p);
}
  • Related