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.