I read this topic, but its problem may be different from mine.
Writing to stdout and file
I want to write a function, this function needs to be printed for both stdout and the file. My C program receives user input scanf.
I intend to write a function like printf, but I really don't know how:
I tried this, but it can only work with a "clean" string, cannot convert% d,%. * lf (my print function needs only two conversions)
void dupPrint(FILE *fp,char *string)
{
printf("%s",string);
fprintf(fp,"%s",string);
return;
}
I tried dup2 and freopen, but they did not work for me.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
int i;
int file = open("input3.txt", O_APPEND | O_WRONLY);
if(file < 0) return 1;
if(dup2(file,1) < 0) return 1;
printf("Redirect to file!\n");
printf("enter i : ");
scanf("%d",&i);
return 0;
}
This tutorial dup2 () only prints to a file.
I also tried tee, but maybe it doesn’t work because I have to get input from the user (if it works, this is not “fair” because tee is not in my program).
, printf- , , . * lf
( )
#include <stdio.h>
#include <stdarg.h>
void dupPrint(FILE *fp,char *fmt, ...)
{
va_list ap;
char *p, *sval;
int ival;
double dval;
va_start (ap, fmt);
for(p = fmt; *p; p++)
{
if (*p != '%') {
putchar(*p);
continue;
}
switch (*++p) {
case 'd':
ival = va_arg(ap, int);
printf("%d", ival);
break;
case '.*lf'
}
}
}
- ?