I want to pass the program output to a .txt file, how would I do it?
I want to pass the program output to a .txt file, how would I do it?
To redirect the output of a program to a file simply call the terminal / command line:
Windows:
programa.exe > arquivo.txt
Unix
./programa > arquivo.txt
Similarly, you can use <
to use a file as input , as if it were what you would type directly into the console for input :
programa < arquivo_entrada.txt
You can even combine the two and use one input file and one output file:
programa < arquivo_entrada.txt > arquivo_saida.txt
If you want to pass the COMPLETE program output to the file, you can do it as follows:
Windows:
meuprograma.exe > arquivo.txt
Unix / Linux:
./meuprograma > arquivo.txt
Note: Only one ">" will overwrite everything that is inside the file, and two "> >" will add. Both will create a file if it does not exist!
But if you want to pass the output from within your program, from a variable for example, you can do it as follows:
char Str[100];
FILE *arq;
arq = fopen("arquivo.txt", "wt"); //abre arquivo ou cria um
if (arq == NULL){ //verifica se ocorreu erro
printf("Erro ao abrir o arquivo\n");
return;
}
strcpy(Str, "testando"); //passa a plavra para variavel
resultado = fputs(Str, arq); //grava variavel no arquivo
if (resultado == EOF){ //verifica se ocorreu erro
printf("Erro na Gravacao\n");
}
fclose(arq); //fecha o arquivo
I do not know if I can help, but I hope so!