I am in the process of writing a program that takes the input of a text file, recognizes all the unique words, and prints them into another text file.
For some reason, when it prints a long list, it sometimes prints random blank lines in between words to the text file, thinking that it's a word. It might be because of subsequent new line characters, but I'm not sure.
Code:#include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> #define ROW 600 #define COL 21 int word_total = 0, line_num = 0, duplicates = 0, num_words = 0, total_word_len = 0; void read_text(FILE *fp1, char (*textArray)[21], char (*wordArray)[21]); void write_text(FILE *fp2, char (*textArray)[21]); void write_average(FILE *fp2, int total_word_len, int word_total); void write_max_min(FILE *fp2, char (*wordArray)[21]); int main(void) { FILE *fp1, *fp2; char textArray[ROW][COL], wordArray[ROW][COL]; if((fp1 = fopen("in.txt", "r")) == NULL) { printf("Unable to read file. Press any key to exit."); getchar(); exit(EXIT_FAILURE); } else { printf("Input file accessed successfully. Scanning stream "); printf("from in.txt.\n"); read_text(fp1, textArray, wordArray); } if((fp2 = fopen("out.txt", "w")) == NULL) { printf("Unable to write file. Press any key to exit."); getchar(); exit(EXIT_FAILURE); } else { printf("Output file accessed successfully. Outputing stream "); printf("to file out.txt.\n"); write_text(fp2, textArray); write_average(fp2, total_word_len, word_total); write_max_min(fp2, wordArray); } printf("The program was able to access all files successfully.\n"); printf("Program executed on %s at %s.\n", __DATE__, __TIME__); fclose(fp1); fclose(fp2); getchar(); return 0; } void read_text(FILE *fp1, char (*textArray)[21], char (*wordArray)[21]) { int i = 0, j = 0, h = 0; char ch = ' '; while((ch = fgetc(fp1)) != EOF) { if((ch == ' ')||(ch == '\n')) { i++; j = 0; } if((ch < 32)||(ch > 64)||((ch < 91)&&(ch > 96)&&(ch < 123))) { textArray[i][j] = tolower(ch); j++; } } word_total = i; /* for(h = 0; h < word_total; h++) { textArray[h][j] = wordArray[h][j]; } */ } void write_text(FILE *fp2, char (*textArray)[21]) { fprintf(fp2, "Number of unique words = %d\n\n", (word_total - duplicates)); int i; for (i = 0; i < (word_total - duplicates); i++) { fprintf(fp2, "%d ", line_num); fprintf(fp2, "%s\n", textArray[i]); line_num++; } fprintf(fp2, "\nTotal number of words = %d", (word_total + duplicates)); } void write_average(FILE *fp2, int total_word_len, int word_total) { double avg; avg = total_word_len / word_total; fprintf(fp2, "\nAverage = %.2f\n", avg); } void write_max_min(FILE *fp2, char (*wordArray)[21]) { }



2Likes
LinkBack URL
About LinkBacks



