0

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.

  • 1
    You can use `popen` for that – jvx8ss Nov 30 '22 at 11:26
  • @jvx8ss the analogue in windows is [_popen](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/popen-wpopen?view=msvc-170) with an underscore – David Ranieri Nov 30 '22 at 12:20
  • @jvx8ss Thank you for your time. Where do I need to use it? Beforde the CMD output or before? Could you give me a example in my case, please? :) – Sergej Wozke Nov 30 '22 at 12:28

3 Answers3

1

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?

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • Thank you for your time. I know there are a lot of better option to do this task, like powershell for example. But I try to learn C and for that I try to give myself tasks. I try to give myself tasks that I could theoretically use in my everyday work. So maybe you could give me an example to better unterstand what you mean. I would appreciate it very much. – Sergej Wozke Nov 30 '22 at 12:36
0

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

#include <stdio.h>

#define BUFSIZE 1000
#define CMDBUFSIZE 100

int main()
{
    char buf[BUFSIZE] = {0}; // Will hold cmd output
    char cmdbuf[CMDBUFSIZE]; // Used to format the cmd
    char *ip = "google.com";

    snprintf(cmdbuf, CMDBUFSIZE, "ping %s /n 1", ip);

    FILE *p = _popen(cmdbuf, "r");
    if (p == NULL) {
        puts("popen failed");
        return 1;
    }

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

    _pclose(p); // Make sure to _pclose so that the cmd doesn't turn into a zombie process
}
jvx8ss
  • 529
  • 2
  • 12
  • Thank you very much, the code also works, but it cannot be changed in such a way that it also does exactly the same as my code. Anyway, I can't get it. When I try to ping with an individual address, it tells me there are too many arguments. The line where I tried to implement this looks like this -> char pingAdr[100]; FILE *Test = _popen("ping %s",pingAdr, "r"); – Sergej Wozke Nov 30 '22 at 15:56
  • @SergejWozke You cant format the first argument of popen, you should `sprintf` first – jvx8ss Nov 30 '22 at 16:21
  • @SergejWozke I will edit the code to show an example of how to format it correctly – jvx8ss Nov 30 '22 at 16:28
0

The easiest way is, to save the output directly inside the CMD command

ping 127.0.0.1 > Test_Log.txt

This saves the output of ping 127.0.0.1 to the file named Test_Log.txt
which is exactly what you want.

Karrer
  • 44
  • 5