I have worked on the program below for countless hours. It is suppose to take the words from a file and count the number of frequencies a word occurs. But I am unable to get the program to iterate through the array. Any ideas?
Thanks for any help or pointers,Code:*******************************************************************************
*Heading - word.c This reads in data from a file entered by the user. Then it*
* selects the ten of the highest number of occurances of words*
* in the file. *
* *
*Author - Mark R. Wilson *
* *
*Issues - The program only calculates the last word in the array. *
* *
*References - Workout C *
* The C Programming Language *
* http://cosmo.mc.edu/moodle/file.php/185/frequency.cpp *
* http://www.daniweb.com/forums/thread91918.html *
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#define SIZE 100000
#define CHAR 31
#define NONALPHA "1 2 3 4 5 6 7 8 9 0 \v\f\n\t\r+=-*/\\,.;:'#~?<>|{}[]`!\"£$%^&()"
int frequency(int words_freq[SIZE][CHAR], int location);
int main (void){
int i = 0;
int fissureCount = 0;
int wordCount = 0;
int theSame = 0;
int tallyCount[SIZE];
int count = 0;
char file_name[50];
char accum[SIZE];
char array[SIZE];
char *p;
char *line = NULL;
char *fissure;
char words[10000][CHAR];
FILE *file;
int frequency(int words_freq[SIZE][CHAR], int location)
{int temp1,temp0;
int i=location,j=0,k=0;
while(((i>0) && (words_freq[i][1]>=words_freq[i-1][1])))
{temp0=words_freq[i][0];temp1=words_freq[i][1];
words_freq[i][0]=words_freq[i-1][0];words_freq[i][1]=words_freq[i-1][1];
words_freq[i-1][0]=temp0;words_freq[i-1][1]=temp1;
i--;};
return 0;
}
//Get the input for the file name and location
printf("Enter the file name to read.\n");
printf("It should look like C:\\\\location\\\\text.txt\n");
printf("Please enter no more than 50 characters.\n");
scanf("%50s",file_name);
//Opens the file name entered by the user.
file = fopen(file_name, "r");
while(file==NULL){
printf("\n\nError: can't open file.\n");
printf("\nPlease verify your file path and name.\n");
printf("\nWhen you enter the file path and name it should look like:\n");
printf("C:\\\\location\\\\text.txt\n");
printf("Please enter no more than 50 characters.\n");
scanf("%50s",file_name);
file = fopen(file_name, "r");
}
if (file !=NULL);
printf("\nYour file was opened succesfully.\n\n\n");
i = 0;
/////The line below was used for testing
//file = fopen("C:\\Temp\\text-testfiles\\ex5.txt", "r");
while(!feof(file)){
fscanf(file, "%s", &array[i]);
i++; //this part should iterate through the array to add words to the
//array, but it is not?
sprintf(array, "%s\n ");
//////////////////////////////Used for testing\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//printf(" %s", array);
char *p = strchr(array,'\n');
if ( p ) *p = '\0';
}
fclose(file);
for (i=0; i<SIZE; i++) array[i] = tolower (array[i]);
fissure = strtok(array,NONALPHA);
for ( i = 0; i < SIZE; i++){
fissure = array;
}
while (fissure != NULL) {
theSame = 0;
count = 0;
while (count < wordCount && !theSame)
if (strcmp (fissure, words[count])==0){
tallyCount[count]++;
theSame = 0;
} else {
count++;
}
if (!theSame) {
strcpy( words[wordCount] , fissure);
tallyCount[wordCount]= 1;
wordCount++;
}
fissureCount++;
fissure = strtok(NULL,NONALPHA);
}
printf ("\n\n WORDS & FREQUENCY \n");
for (count = 0; count<wordCount; count++){
printf (" %-25s %d \n", words[count],tallyCount[count]);
}
char z;//This variable holds the any key entered
printf("\n\n To exit, please press a key and then press enter.");
scanf("%c%c",&z,&z); //The %c is to scan in a single character
return 0;
}
Dane771