0

I have a program that is supposed to take three command line arguments and read from a file, a list of words, then count the frequency of each word in the file then print a list of the words and their count to an output file. So far the program is compiling but when I run the program it does not output to a file and just hangs. I have looked over it for several hours but cannot come to a conclusion as to why it is hanging so I thought I would ask here. I am very new to C so I dont understand it quite as well as I should.

Command to be entered when running the program

proj1 proj1.in proj1.out

Code:

#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.

If anyone could give me some pointers as to what I'm doing wrong I'd really appreciate it. Thanks in advance

EDIT: As others have suggested I changed scanf to fscanf. However now I get a segfault error when I try to run the program.

EDIT 2: Segfault was a mistake in the command line argument by me. Program is working now.

bkedge
  • 105
  • 1
  • 2
  • 10
  • 2
    Starring down the code may or may not help. A debugger is usually more reliable. Use one. – n. m. could be an AI Sep 25 '14 at 20:25
  • 1
    You can start with two lines `while(!feof(finput)) { scanf("%s", s);`. They contain two errors. One is explained [here](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). The other one is that `scanf` doesn't read from the file you want to read from. You need `fscanf`. Once you fix these, you can progress more easily. – n. m. could be an AI Sep 25 '14 at 20:36
  • @n.m. I see. I fixed `scanf` and I'm looking at your example on `feof`. My code still compiles but now I get a seg fault when I try to run. I'll look into using a debugger but we just began going over that today so it may take a while. Thanks for your input – bkedge Sep 25 '14 at 20:46
  • If you fixed some of the errors, update the post. – n. m. could be an AI Sep 25 '14 at 20:48
  • I get a segfault if I run your program without arguments -- *first thing* you should do is check the number of arguments. After changing `scanf` to `fscanf` it works for me. – Jongware Sep 25 '14 at 21:15
  • @Jongware oddly enough i'm not getting a segfault error anymore. I may have messed something up in the commands. Thanks for the help. – bkedge Sep 25 '14 at 21:21
  • Kudos for updating your post by appending data rather than delete/add. Easier for late comers to see the evolution of the post. – chux - Reinstate Monica Sep 26 '14 at 04:09

0 Answers0