![]() |
1
8
永远不要使用GET!! 7.19.7.2 The fgets function Synopsis 1 #include <stdio.h> char *fgets(char * restrict s, int n, FILE * restrict stream); Description 2 The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array. Returns 3 The fgets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.
所以,
对
当
如果
编辑 示例添加 $ cat fgets_error.c #include <stdio.h> void print_buf(char *buf, size_t len) { int k; printf("%02X", buf[0]); for (k=1; k<len; k++) printf(" %02X", buf[k]); } int main(void) { char buf[3] = {1, 1, 1}; char *r; printf("Enter CTRL+D: "); fflush(stdout); r = fgets(buf, sizeof buf, stdin); printf("\nfgets returned %p, buf has [", (void*)r); print_buf(buf, sizeof buf); printf("]\n"); return 0; } $ ./a.out Enter CTRL+D: fgets returned (nil), buf has [01 01 01] $ 看到了吗?buf中没有nul:) |
![]() |
2
9
从不使用
|
![]() |
3
4
曼菲斯: fgets()从流中最多读取一个小于大小的字符,并将它们存储到s指向的缓冲区中。在eof或换行符之后,读取将停止。如果读取新的_行,则将其存储到缓冲区中。 在缓冲区中的最后一个字符之后存储了一个'\0'。 |
![]() |
4
0
是的。从 CPlusPlus.com
|
![]() |
5
0
如果您确实以二进制模式“rb”打开了文件,并且如果您希望使用fgets逐行读取文本,则可以使用以下代码来保护您的软件不丢失文本,如果错误地文本包含一个\0'字节。
但最后就像其他人提到的,通常你不应该使用
|
![]() |
MaPo · Linux,设置锁定ICMP_过滤器选项 4 月前 |
![]() |
Doohyeon Won · 内联函数上的奇怪现象?[关闭] 4 月前 |
![]() |
Bobby · 复合字面值总是左值吗? 4 月前 |
![]() |
9-Pin · C: 嵌套结构的堆栈内存分配 4 月前 |