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

标签 c kernighan-and-ritchie

<分区>

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

我开始做这个练习,但我卡在了开头。我试图获取整个输入并将其逐行保存在数组中,然后将数组中的指针指向保存的行。后来想到给每个词分配空间,copy到一棵树上。但是,在我作为输入输入时的以下代码中:

abc def;'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;

}

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

最佳答案

将这一行添加到主函数的开头

freopen("filename.txt", "r", stdin);

它将重定向所有标准输入以从文件中读取。那会让你省去很多麻烦。

关于c - c 程序的意外行为,试图解决 E6-2 K&R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34664304/

相关文章:

c - 接受 int 并转换为数组的函数向后插入值?

c - printf 结构体成员

c - 这个c程序没有给出正确的输出。(将数字字符串转换为整数)

c - c语言编程中如何用一个空格代替多个空格?

c - 如何模拟 EOF?

有人可以解释这个 OpenCL 程序有什么问题吗?

c - C 中带有 char 的 Switch 语句总是返回 0

c - mac osx 终端中的信号 EOF

c - K 和 R C 编程练习 1-19