你实际上跳过了第一行。
由于您从 STDIN 读取,因此您键入的内容没有文件。您的输出和输入混淆了。我们可以通过更改您的 printf 添加前缀 printf("output: %s", line); 来看到这一点。
A B C <-- this is your input echoed to the screen
D E F <-- and this
output: D E F
output:
您的代码正在读取第一行,检查它的长度,然后读取下一行而不打印第一行。这就是为什么你错过了第一行。
我们在最后得到了额外的空白打印,因为您正在检查您是否从 previous 行中读取了任何内容。然后您无需检查即可立即阅读和打印。
// Read the first line.
line_size = getline(&line, &line_buf_size, stdin);
int row = 0;
// Check if you read anything from the *previous* line.
while (line_size >= 0)
{
row++;
// Read the next line overwriting the first line.
line_size = getline(&line, &line_buf_size, stdin);
// Print the second and subsequent lines without first checking
// if you read anything.
printf("%s", line);
}
相反,阅读、检查和打印。
#include <stdio.h>
int main() {
char *line = NULL;
size_t line_buf_size = 0;
int row = 0;
// Read and check.
while (getline(&line, &line_buf_size, stdin) > -1)
{
row++;
// Print.
printf("output: %s", line);
}
}
我们得到交错的输入和输出。
A B C
output: A B C
D E F
output: D E F
您不需要存储行长度,但如果您在比较之前确实在分配周围放置了括号。这确保了 (line_size = getline(...)) > -1 而不是 line_size = (getline(...) > -1)。那就是将 getline 的返回值存储在 line_size 中,then 检查它是否为 -1。不检查 getline 是否返回 -1 并将真/假结果存储到 line_size。
while((line_size = getline(&line, &line_buf_size, stdin)) > -1)