Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c程序的意外行为,试图从K&;解决E6-2问题;R_C_Kernighan And Ritchie - Fatal编程技术网

c程序的意外行为,试图从K&;解决E6-2问题;R

c程序的意外行为,试图从K&;解决E6-2问题;R,c,kernighan-and-ritchie,C,Kernighan And Ritchie,练习6-2。编写一个程序,读取C程序并按字母顺序打印每组数据 变量名,前6个字符相同,但其后某个地方不同。不算数 字符串和注释中的单词。使6成为可从命令行设置的参数。(来自K&R) 我开始做这个练习,但我还停留在开始阶段。我试图获取整个输入并将其逐行保存在数组中,然后将数组中的指针指向保存的行。后来我想到为每个单词分配空间,并将它们复制到一棵树上。但是,在以下代码中,当我输入时: abc def;'Ctrl-Z' #包括 #包括 #包括 #定义MAXLINE 100 #定义MAXWORDS 10

练习6-2。编写一个程序,读取C程序并按字母顺序打印每组数据 变量名,前6个字符相同,但其后某个地方不同。不算数 字符串和注释中的单词。使6成为可从命令行设置的参数。(来自K&R)

我开始做这个练习,但我还停留在开始阶段。我试图获取整个输入并将其逐行保存在数组中,然后将数组中的指针指向保存的行。后来我想到为每个单词分配空间,并将它们复制到一棵树上。但是,在以下代码中,当我输入时:

abc def;'Ctrl-Z'

#包括
#包括
#包括
#定义MAXLINE 100
#定义MAXWORDS 10
int read_输入(字符*[]);
int main(int argc,char*argv[])
{
char*变量[MAXWORDS]={0};
int i;
int tmp;
while((tmp=read_输入(变量))!=EOF){
对于(i=0;i0&&c==EOF)
结束=EOF;
else如果(i==0&&c==EOF){
printf(“!”);
返回EOF;
}
其他的
返回i;
}

程序不会立即停止,而main的while循环会请求另一个输入。。。我做错了什么?

你什么也没做错。由于C stdio库函数的通常设计,只有在前面没有未读输入的情况下才能识别EOF,因此您必须键入Enter Ctrl-Z或Ctrl-ZCtrl-Z。

您没有做错什么。由于C stdio库函数的通常设计,只有在前面没有未读输入的情况下才能识别EOF,因此必须键入Enter Ctrl-Z或Ctrl-ZCtrl-Z。

EOF
不是这样键入的,它是作为变量值进行测试的定义值。在控制台中键入时,在Linux中键入
Ctrl-D
以创建文件结尾,在Windows中则是
Ctrl-Z
。读取磁盘文件时,系统使用它来表示文件中没有更多数据
EOF
不是文件中的字符,它是库函数用于通知由于到达文件结尾而导致操作失败的常数。要使用
EOF
NULL
。。您需要在top@WeatherVane我不知道如何显示Ctrl-Z,所以我只键入了“EOF”。我最初在cmd中键入“a”;Ctrl-Z'
EOF
不是那样键入的,它是作为变量值进行测试的定义值。在控制台中键入时,在Linux中键入
Ctrl-D
以创建文件结尾,在Windows中则是
Ctrl-Z
。读取磁盘文件时,系统使用它来表示文件中没有更多数据
EOF
不是文件中的字符,它是库函数用于通知由于到达文件结尾而导致操作失败的常数。要使用
EOF
NULL
。。您需要在top@WeatherVane我不知道如何显示Ctrl-Z,所以我只键入了“EOF”。我最初在cmd中键入“a”;Ctrl-Z'
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAXLINE 100
#define MAXWORDS 10

int read_input(char *[]);

int main(int argc, char *argv[])
{
    char *variables[MAXWORDS] = {0};
    int i;
    int tmp;


    while((tmp = read_input(variables)) != EOF){
        for(i=0; i < tmp; i++){
            printf("%s ",*(variables+i));
        }
    }


    return 0;
}


/* read_input: mapping all the input words into *variables[], and returning the number of the words + 1 */
int read_input(char *variables[]){
    static char s[MAXLINE];
    int tmp;
    int i, j;

    i = 0;
    if((tmp = getline(s, MAXLINE)) == EOF)
        return EOF;

    for(j = 0; j < MAXWORDS && s[i] != '\0'; j++){ /* it does'nt work when j = MAXWORDS because the rest of the line is not saved */
        while(!isalpha(s[i]))
            i++;
        *(variables+j) = &s[i];
        while(isalnum(s[i]) || s[i] == '_')
            i++;
        if(s[i] == '\0')
            continue;
        s[i++] = '\0';
    }
    return j;
}

/* getline: read a line into s, return length */
int getline(char *s,int lim)
{
    static char end; /* static typed char that saves if the last not saved 'c' was an EOF */
    char c;
    int i;
    if(end == EOF){ /* the last input character was an EOF */
        end = 0;
        return EOF;
    }
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=';'; ++i)
        s[i] = c;
    s[i] = '\0';
    if(i > 0 && c == EOF)
        end = EOF;
    else if(i == 0 && c == EOF){
        printf("!");
        return EOF;
    }
    else
        return i;

}