gpt4 book ai didi

c - 当输入是通过 shell 时,如何找到 C 中输入的长度?

转载 作者:行者123 更新时间:2023-11-30 15:12:09 25 4
gpt4 key购买 nike

比方说,用户输入

echo abc, de | ./test

在 shell 中(已经编译了 test.c),如何获取输入的长度(在本例中为 7)?

最佳答案

计算标准输入中的字符数。

int main() {
int n = 0;
while (getchar() != EOF) {
n++;
}
n--; // Ignore final newline
printf("%d chars\n", n);
return 0;
}

关于c - 当输入是通过 shell 时,如何找到 C 中输入的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35235681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com