0
I want to pass the program output to a . txt file, as I would?
2
To redirect the output of a program to a file just call the right way from the terminal/command line:
Windows:
programa.exe > arquivo.txt
Unix
./programa > arquivo.txt
Analogously can use <
to use a file such as stream input, or as if it were what you would write directly on the console to input:
programa < arquivo_entrada.txt
You can even combine the two and use an input and output file:
programa < arquivo_entrada.txt > arquivo_saida.txt
1
If you want to pass the FULL output of the program to the file, you can do as follows:
Windows:
meuprograma.exe > arquivo.txt
Unix/Linux:
./meuprograma > arquivo.txt
Note: Only one ">" will replace everything 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 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 don’t know if I can help, but I hope so!
Browser other questions tagged c filing-cabinet txt
You are not signed in. Login or sign up in order to post.
I started studying c this year and I’m still right at the beginning, if you can suggest me which commands and libraries to use to resolve my question above, I would appreciate it very much
– Temp Account
Possible duplicate of Read file in C
– Woss
@Andersoncarloswoss So if a doubt has already been resolved in any other post I can no longer ask about it?
– Temp Account
If it is answered there, you have no reason to ask again - and get the same answer. If the answer there is not what you need, make it clear in the question what you need that the current answer does not answer.
– Woss
@Andersoncarloswoss It is because of course the words I used to ask are not the same that another person used, so how to know on a future occasion if the question has already been answered? Remembering that not always the search questions works perfectly, as I said, I will search using the words x and the person may have asked using words y.
– Temp Account
In these cases, when you searched but did not find it, open the question without fear. If the answer exists, someone will know and signal as duplicate. It’s not bad to have the duplicate question - they even help optimize searches: just as you didn’t find the answer, others might not, but now they would find your question.
– Woss
@Andersoncarloswoss Got it, and one last question, if you can answer clear, today I did a post, which I thought was in the patterns on the site, and was reported as off-topic, was asking what would be the ideial language to do a kind of program there, and even the only one who answered me also received many downvotes, which I really did not understand, that would apply to me who asked the wrong question and not to him who answered correctly. Then I would like to know: How can I see if my question is within the standards? Because just being related to programming doesn’t seem to be enough.
– Temp Account
Many of your doubts have already been discussed in the [meta] or are already described in the [tour].
– Woss
@Andersoncarloswoss But I only posted two questions
– Temp Account
Doubts you raised in the comments regarding the operation of the site, not regarding your questions.
– Woss
@Andersoncarloswoss Oh I understand, I’m sorry
– Temp Account
Personally I could not understand the question. Do you want to save information in a file . txt ? or make what goes out to the console (by
printf
) be redirected to a file ? And what do you mean by "import data for the execution of the program into an external file" ?– Isac
@Isac actually I took a txt as an example, I think it’s easier to start with text files. Forget the "import data... external", how would I export output to a text file? Pretend that was my question
– Temp Account
@That’s what Isac was, thank you
– Temp Account