我只想将其写入 1 行:
fprintf(stdout, "RCPT TO: <%s>\r\n", argv[argc-1]);
fprintf(sockfd, "RCPT TO: <%s>\r\n", argv[argc-1]);
所以我想将相同的字符串发送到标准输出和我打开的套接字。我该怎么做?
最佳答案
与
#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;
}
可以
fprintf_both(stdout, sockfd, "RCPT TO: <%s>\r\n", argv[argc-1]);
关于c - 在 1 行中将字符串发送到标准输出和套接字的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2123299/