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.