我写了一个 C 程序。 (页面底部的问题)我可以提供实际代码,但我认为没有必要回答这个问题。
Used a while loop with fgets to get sizeof line
Assigned that fgets to line[255] in the fgets
Assigned the line to a char* (inside the loop)
Printf the char* (also inside the loop)
C 程序按预期输出。
我使用 strace -o x.txt ./a.out 查看幕后发生的事情。
我看到了这个:(当然上面/下面还有很多乱码我不明白)
read(3, "text\nMore text\nEven more text"..., 4096) = 72
write(1, "text\n",5) = 5
... more of the write() = #
read(3, "", 4096) = 0
close(3)
问题:
我得到了
write(1=stdout, "text to print", #of char)
或者这是字节数?我不明白
read(3, "", 4096) = 0
我知道0=stdin
,1=stdout
,2=stderr
,不知道3是什么意思——也许就是这个文件?
我不知道为什么要进行另一次读取,我假设 4096 是什么缓冲区大小?
我最好的猜测是因为它说 = 0 因为它的 EOF ?
最佳答案
字符数
您打开的文件。 4096 是缓冲区大小。上次读取未能读取任何字节(0 字节)。
关于c编程理解strace -o t.txt ./a.out,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12750885/