将字符串发送到1行的stdout和socket的方法

Ber*_*chi 0 c unix linux stdout

我想把它写成只有一行:

fprintf(stdout, "RCPT TO: <%s>\r\n", argv[argc-1]);
fprintf(sockfd, "RCPT TO: <%s>\r\n", argv[argc-1]);
Run Code Online (Sandbox Code Playgroud)

所以我想将相同的字符串发送到stdout和我的开放套接字.我怎样才能做到这一点?

Gre*_*con 5

#include <stdarg.h>

int fprintf_both(FILE *a, FILE *b, const char *fmt, ...)
{
  FILE *f[2];
  const int n = sizeof(f) / sizeof(f[0]);
  int i;
  int sum = 0;

  f[0] = a;
  f[1] = b;

  for (i = 0; i < n; i++) {
    va_list ap;
    int bytes;

    va_start(ap, fmt);
    bytes = vfprintf(f[i], fmt, ap);
    va_end(ap);

    if (bytes < 0)
      return bytes;
    else
      sum += bytes;
  }

  return sum;
}
Run Code Online (Sandbox Code Playgroud)

您可以

fprintf_both(stdout, sockfd, "RCPT TO: <%s>\r\n", argv[argc-1]);
Run Code Online (Sandbox Code Playgroud)