学习 C - 为什么我的命令行要求输入。我没有 getchar()
Learning C - why does my command line ask for input. I do not have a getchar()
嘿,我正在学习 C 并尝试打印文件中的最长行。
我正在使用 ANSI C 书中的代码。
我不明白为什么,当我 运行 代码时,命令行似乎在等待输入。据我所知,我没有 getchar() 或其他输入函数。
这是代码。有一个主要功能和另外两个功能
#include <stdio.h>
#define MAXLINE 1000 /*Maximum input line size*/
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/*Print longest input line*/
main()
{
FILE *fpointer;
int len; /*current line length*/
int max; /*maximum lenght seen so far*/
char line[MAXLINE]; /*current input line*/
char longest[MAXLINE]; /*longest line saved here*/
if ((fpointer=fopen("arrays.c", "r")) == NULL) {
printf("Error opening file");
return 1;
}
fgets(line, MAXLINE, fpointer);
max = 0;
while ((len = getline(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
fgets(line, MAXLINE, fpointer);
}
if (max > 0) /*There was a line*/
printf("%s", longest);
return 0;
}
/*getline: read a line into s, return length*/
int getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && c != '\n'; ++i) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '[=10=]';
return i;
}
/*copy: copy 'from' into 'to'; assume to is big enough*/
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '[=10=]')
++i;
}
问题是您未能检查 fgets
的 return 值。当某些东西可以被读取时,它 return 在复制输入后它的缓冲区,并且在文件末尾它 returns NULL 并保持缓冲区不变 .
因此,getline
永远不会 return 0 并且你有一个无限循环。
但这还不是全部,getline
显然是错误的。如所写,它将 return 一致 999 (lim - 1
) 如果 意外 c
的初始值不是 \n
否则0.
最少的修复:
...
if (NULL == fgets(line, MAXLINE, fpointer)) return 0; // immediately stop if file is empty
max = 0;
while ((len = getline(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
if (NULL == fgets(line, MAXLINE, fpointer)) break; //exit loop on EOF
...
int getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c = s[i]) != '\n'; ++i);
...
嘿,我正在学习 C 并尝试打印文件中的最长行。 我正在使用 ANSI C 书中的代码。 我不明白为什么,当我 运行 代码时,命令行似乎在等待输入。据我所知,我没有 getchar() 或其他输入函数。
这是代码。有一个主要功能和另外两个功能
#include <stdio.h>
#define MAXLINE 1000 /*Maximum input line size*/
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/*Print longest input line*/
main()
{
FILE *fpointer;
int len; /*current line length*/
int max; /*maximum lenght seen so far*/
char line[MAXLINE]; /*current input line*/
char longest[MAXLINE]; /*longest line saved here*/
if ((fpointer=fopen("arrays.c", "r")) == NULL) {
printf("Error opening file");
return 1;
}
fgets(line, MAXLINE, fpointer);
max = 0;
while ((len = getline(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
fgets(line, MAXLINE, fpointer);
}
if (max > 0) /*There was a line*/
printf("%s", longest);
return 0;
}
/*getline: read a line into s, return length*/
int getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && c != '\n'; ++i) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '[=10=]';
return i;
}
/*copy: copy 'from' into 'to'; assume to is big enough*/
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '[=10=]')
++i;
}
问题是您未能检查 fgets
的 return 值。当某些东西可以被读取时,它 return 在复制输入后它的缓冲区,并且在文件末尾它 returns NULL 并保持缓冲区不变 .
因此,getline
永远不会 return 0 并且你有一个无限循环。
但这还不是全部,getline
显然是错误的。如所写,它将 return 一致 999 (lim - 1
) 如果 意外 c
的初始值不是 \n
否则0.
最少的修复:
...
if (NULL == fgets(line, MAXLINE, fpointer)) return 0; // immediately stop if file is empty
max = 0;
while ((len = getline(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
if (NULL == fgets(line, MAXLINE, fpointer)) break; //exit loop on EOF
...
int getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c = s[i]) != '\n'; ++i);
...