Piping terminal output to file from within C program -
i gets output stdout saved in file in c code. know can calling process on command line , piping file:
myprogram.exe 1>logfile.txt for example. wondering if there way within c code itself. there function in printf() family can output both terminal , specified file same arguments?
if not, syntax required write own printf()-style function calls both printf() , fprintf() within it, using same argument style printf()?
you can use fprintf() function, quite similar printf() in way works.
here example:
file *fp; int var = 5; fp = fopen("file_name.txt", "w");// "w" means going write on file fprintf(fp, "writting file. int variable: %d", var); the output on file this:
this being written in file. int variable: 5 n.b: opening file using w parameter destroy file's content every time open it.
for writing file have use file operation commands, not possible write file using printf (it prints stdout). can use:
sprintf(buf,"%d",var); //for storing in buffer printf(buf); //output stdout fputs(buf, fp); //output file
Comments
Post a Comment