提问人:couscous 提问时间:11/4/2023 最后编辑:couscous 更新时间:11/4/2023 访问量:63
在 stdin 上使用 read() 检查“\n”(回车)后,我无法打印 f() 或 write() 我的数组
I can't printf() or write() my array after using a read() on stdin to check for '\n' (enter)
问:
我正在编写一个小程序,它应该打印出 sin(counter),而每次我在控制台中输入“\n”(Enter)时,counter 都会增加 0.01。
两者都不要打印任何内容到控制台。
通过调试,我发现我的代码的其余部分正在按预期工作,就在打印 sin_output[0] 之前,它填充了正确的 sin(counter) 值。printf("%lf", sin_output[0]);
write(1, &sin_output, sizeof(sin_output));
我选择了一个数组,因为我希望程序稍后写入套接字或管道,据我所知,printf() 对此毫无用处。
我尝试在一个小程序中重现此错误,如下所示:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main() {
double array[1];
array[0] = 1;
printf("%lf", array[0]);
return 0;
}
这将打印出正确的值。 输出: 1.000000
替换为会给出一些随机字符,这可能是由于 write() 将我的输出格式化为字符串?printf("%lf", array[0]);
write(1, array, sizeof(array));
使用打印相同的胡言乱语。write(1, &array, sizeof(array));
现在我试过:array[0] = sin(x);
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main() {
double array[1];
double x = 0.01
array[0] = sin(x);
printf("%lf", array[0]);
return 0;
}
出于某种原因打印 0.010000(四舍五入?
使用打印出正确的值。double x = 0.1
因此,我在最小的可重复系统中尝试了我所知道的所有选项。所有程序都至少输出一些东西,无论是错误的还是不需要的值。
但是,在我的代码中使用(几乎)完全相同的结构不会打印任何值。没有。array[1] = sin(enter_counter)
我怀疑这是由于我使用并且程序使用 stdin/stdout 缓冲区产生了一些不需要的行为,这导致 stdout 无法正确打印 for 和 。read(0, &buf, sizeof(buf))
printf
write()
我实际程序的代码是:
/* includes*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/* main function*/
int main() {
/* declare variables*/
char buf[1];
double enter_counter = 0;
double sin_output[1];
/* reset buf*/
memset(&buf, 0, 1);
/* master loop*/
while (1) {
/* read stdin*/
read(0, &buf, sizeof(buf));
/* if stdin == enter key, progress sin by 0.01rad*/
if (buf[0] == 10) {
enter_counter += 0.01;
}
/* reset buffer*/
memset(&buf, 0, 1);
/* calculate sin for enter_counter*/
sin_output[0] = sin(enter_counter);
/* printf sin(enter_value)*/
printf("%lf", sin_output[0]);
//write(1, sin_output, sizeof(sin_output));
}
}
任何帮助都是值得赞赏的。
答:
您需要在打印语句中添加换行符,如下所示:
printf("%lf\n", sin_output[0]);
这为我带来了以下结果:
$ ./a.out
0.010000
0.019999
0.029996
0.039989
0.049979
0.059964
^C
不确定到底发生了什么,但很可能你的输出正在被缓冲,然后被你自己的换行符覆盖。查看输出缓冲以更好地理解。
评论
read
并且只处理原始二进制数据。没有文本转换。write
printf()
fflush()
array[0]
double
"1.0"
double
write()
"3.14159"
fprintf()
int
1
int
0x00 0x00 0x00 0x01
0x01 0x00 0x00 0x00
NUL
0x00
SOH
0x01