编写一个程序来确定可以从键盘读取的最大字符数,用于特定的 'fgets()' 函数调用
Write a program to determine the maximum number of characters that can be read from the keyboard, for a specific 'fgets()' function call
代码块包含一个 'fgets()' 函数。我的程序将如何解析代码并确定可以从键盘读取的最大长度?
#include <stdio.h>
int main(void) {
char buffer[128], whole_line = 1, *ch;
while (fgets(buffer, sizeof buffer, stdin)) {
if (whole_line) {
putchar('>');
if (buffer[0]!='>') {
putchar(' ');
}
}
fputs(buffer, stdout);
for (ch = buffer; *ch && *ch!='\n'; ++ch);
whole_line = *ch == '\n';
}
if (!whole_line) {
putchar('\n');
}
return 0;
}
#include <stdio.h>
int main(void) {
int max_length = 0;
int current_length = 0;
char buffer[128], whole_line = 1, *ch;
while (fgets(buffer, sizeof buffer, stdin)) {
if (whole_line) {
putchar('>');
if (buffer[0]!='>') {
putchar(' ');
}
}
fputs(buffer, stdout);
for (ch = buffer; *ch && *ch!='\n'; ++ch, ++current_length);
whole_line = *ch == '\n';
printf("Length: %d\n", current_length);
if(max_length < current_length) {
max_length = current_length;
printf("Max length: %d\n", max_length);
}
current_length = 0;
}
return 0;
}
当当前文本长度大于最大长度时,将当前指定为最大长度。然后它输出 max 到 console
代码块包含一个 'fgets()' 函数。我的程序将如何解析代码并确定可以从键盘读取的最大长度?
#include <stdio.h>
int main(void) {
char buffer[128], whole_line = 1, *ch;
while (fgets(buffer, sizeof buffer, stdin)) {
if (whole_line) {
putchar('>');
if (buffer[0]!='>') {
putchar(' ');
}
}
fputs(buffer, stdout);
for (ch = buffer; *ch && *ch!='\n'; ++ch);
whole_line = *ch == '\n';
}
if (!whole_line) {
putchar('\n');
}
return 0;
}
#include <stdio.h>
int main(void) {
int max_length = 0;
int current_length = 0;
char buffer[128], whole_line = 1, *ch;
while (fgets(buffer, sizeof buffer, stdin)) {
if (whole_line) {
putchar('>');
if (buffer[0]!='>') {
putchar(' ');
}
}
fputs(buffer, stdout);
for (ch = buffer; *ch && *ch!='\n'; ++ch, ++current_length);
whole_line = *ch == '\n';
printf("Length: %d\n", current_length);
if(max_length < current_length) {
max_length = current_length;
printf("Max length: %d\n", max_length);
}
current_length = 0;
}
return 0;
}
当当前文本长度大于最大长度时,将当前指定为最大长度。然后它输出 max 到 console