Word Concordance程序挂起并且没有输出

时间:2014-09-25 20:19:49

标签: c input linked-list frequency

我有一个程序应该采用三个命令行参数并从文件读取,一个单词列表,然后计算文件中每个单词的频率,然后打印单词列表及其计数到输出文件。到目前为止程序正在编译,但是当我运行程序时,它不会输出到文件而只是挂起。我已经查了好几个小时但是无法得出结论为什么它悬挂所以我想我会问这里。我对C很新,所以我不太了解它。

运行程序时要输入的命令

proj1 proj1.in proj1.out

代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define MAXLENGTH 100
#define MAXWORDS 10000

/* STRUCTURE */
typedef struct word {

    char s[MAXLENGTH];

    int count;

} word;

/*  PROTOTYPES  */
int  wordcmp(word *a, word *b);
void make_all_lower(char *s);
void remove_char(char *s, int i);
void remove_nonalpha(char *s);
void wordinsert(word *words, int *n, char *s);


int main(int argc, char *argv[]) {
    word words[MAXWORDS];

    char s[1000];

    char inFile[1024];

    char outFile[1024];

    int i, n, m;

    strlcpy(inFile, argv[1], 1024);

    strlcpy(outFile, argv[2], 1024);

    if(argc != 3)
    {
        printf("Please enter the correct number of arguments\n");
    }

    else
    {
        FILE *finput, *foutput;

        finput = fopen(inFile, "r");
        foutput = fopen(outFile, "w");

        //Holds number of total words
        n = 0;

        while(!feof(finput))
        {

            scanf("%s", s);

            if(isalpha(s[0]))
            {
                remove_nonalpha(s);
                make_all_lower(s);
                wordinsert(words, &n, s);
            }
    }


    //Sort routine for the structure
    qsort((void *) words, n, sizeof(word), (int (*) (const void *, const void *)) wordcmp);

    for(i = 0; i < n; i++) 
    {
        fprintf(foutput, "%s\t%d\n", words[i].s, words[i].count);
    }

    fclose(finput);
    fclose(foutput);


    }

return 0;

}


/*FUNCTIONS */
void wordinsert(word *words, int *n, char *s)
{
    int i;

    for(i = 0; i < *n; i++)
    {
        if(strcmp(s, words[i].s) == 0)
        {
            words[i].count++;
            return;
        }
    }

    strcpy(words[*n].s, s);

    words[*n].count = 1;

    (*n)++;

}

int wordcmp(word *a, word *b)
{
    if(a->count < b->count)
        return +1;
    if(a->count > b->count)
        return -1;
    return 0;
}


//removes characters from string
void remove_char(char *s, int i)
{
    while(s[i])
    {
        i++;
        s[i-1] = s[i];
    }

    s[i] = 0;
}

void remove_nonalpha(char *s)
{
    int i;

    for(i = 0; s[i]; i++)
    {
        if(!isalpha(s[i]))
        {
            remove_char(s, i);
        }
    }
}


void make_all_lower(char *s)
{
    int i;

    for(i = 0; s[i]; i++)
    {
        s[i] = tolower(s[i]);
    }
}

proj1.in:

after you have about five or six hours and are proficient in the four
fundamentals: stalls, the wind correction maneuvers, and elementary
emergencies, the period of shooting takeoffs and landings will begin.
Maybe you feel that takeoffs and landings are the most important parts of
flying, but as far as your training is concerned there are just another
maneuver.
The average sutdent even places more weight on landings than takeoffs.
Takeoffs are not important, he thinks, but landings are.
So with this attitude he finds himself having plenty of trouble when it
comes to getting the bird in the air.
The instructor will have you follow him through on the takeoffs during the
first flight or two, and probably by the third or fourth flight you will be
making most of the takeoffs yourself.
Most of the trainers being used thoday are the tricycle gear types.
As was the case for taxiing, the lower nose position gives better visibility
during the ground roll parts of the takeoff and landing.

如果有人能给我一些关于我做错的指示,我真的很感激。提前致谢

编辑:其他人建议我将scanf更改为fscanf。但是,当我尝试运行程序时,现在出现了段错误。

编辑2:Segfault在我的命令行参数中是一个错误。该计划现在正在运作。

0 个答案:

没有答案