![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
Help with sorting and counting?
Well, so far I have this program reading the first 10 words of a file. But what I need it to do is read all of the words, sort the words, count the words, and print the first and last 10 words of the unsorted, then read the first and last 10 words of the sorted. So as you can see I'm so far away. But what I need help with right now is sorting all the words. Here's the fragment of my code that reads from the file. Any suggestions?
if( myfile != NULL)
{
for(a=0; a<10; a++)
{
/* Get one line */
fgets(Buffer, 5000, myfile);
if (!feof(myfile))
{
/* Break the line up into words */
token = strtok(Buffer, delimiters);
while (token != NULL)
{
puts(token);
/* Get the next word */
token = strtok(NULL, delimiters);
}
}
}
fclose( myfile );
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4
![]() |
Could you post all of the code you have so far?
![]() btw...I love strawberries
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#3 |
|
Newbie
|
Yeah, but I think I'm going about this all wrong. Right now it's just reading then printing the first 10 words. But I need to store them. But every time I use something like temp[a] = strtok( Buffer, delimiters) or something it messes up completely. How can I store each of the words?
#include <stdio.h>
#include <string.h>
int main( )
{
FILE *myfile = NULL;
char Buffer[5000][20];
char Temp[5000][20];
int wordcount[5000];
char *delimiters = ",. \t\n";
char *token;
int q=1;
int a;
int fileselect;
do
{
printf( "\nSelect the number of the text file to read\n");
printf( "1 - words_emancip\n");
printf( "2 - words_flatland\n");
printf( "3 - words_macbeth\n");
printf( "4 - words_prince\n");
printf( "5 - words_purloin\n\n");
fflush( stdin);
scanf( "%d", &fileselect);
printf( "\n");
{
switch( fileselect)
{
case 1:
myfile = fopen( "words_emancip.txt", "r" );
if( myfile != NULL)
{
for(a=0; a<10; a++)
{
/* Get one line */
fgets(Buffer, 5000, myfile);
if (!feof(myfile))
{
/* Break the line up into words */
token = strtok(Buffer, delimiters);
while (token != NULL)
{
puts(token);
/* Get the next word */
token = strtok(NULL, delimiters);
}
}
}
fclose( myfile );
}
else
{
printf( "Cannot open file.\n");
printf( "Goodbye!\n");
}
break;
case 2:
myfile = fopen( "words_flatland.txt", "r" );
if( myfile != NULL)
{
for(a=0; a<10; a++)
{
/* Get one line */
fgets(Buffer, 5000, myfile);
if (!feof(myfile))
{
/* Break the line up into words */
token = strtok(Buffer, delimiters);
while (token != NULL)
{
puts(token);
/* Get the next word */
token = strtok(NULL, delimiters);
}
}
}
fclose( myfile );
}
else
{
printf( "Cannot open file.\n");
printf( "Goodbye!\n");
}
break;
case 3:
myfile = fopen( "words_macbeth.txt", "r" );
if( myfile != NULL)
{
for(a=0; a<10; a++)
{
/* Get one line */
fgets(Buffer, 5000, myfile);
if (!feof(myfile))
{
/* Break the line up into words */
token = strtok(Buffer, delimiters);
while (token != NULL)
{
puts(token);
/* Get the next word */
token = strtok(NULL, delimiters);
}
}
}
fclose( myfile );
}
else
{
printf( "Cannot open file.\n");
printf( "Goodbye!\n");
}
break;
case 4:
myfile = fopen( "words_prince.txt", "r" );
if( myfile != NULL)
{
for(a=0; a<10; a++)
{
/* Get one line */
fgets(Buffer, 5000, myfile);
if (!feof(myfile))
{
/* Break the line up into words */
token = strtok(Buffer, delimiters);
while (token != NULL)
{
puts(token);
/* Get the next word */
token = strtok(NULL, delimiters);
}
}
}
fclose( myfile );
}
else
{
printf( "Cannot open file.\n");
printf( "Goodbye!\n");
}
break;
case 5:
myfile = fopen( "words_purloin.txt", "r" );
if( myfile != NULL)
{
for(a=0; a<10; a++)
{
/* Get one line */
fgets(Buffer, 5000, myfile);
if (!feof(myfile))
{
/* Break the line up into words */
token = strtok(Buffer, delimiters);
while (token != NULL)
{
puts(token);
/* Get the next word */
token = strtok(NULL, delimiters);
}
}
}
fclose( myfile );
}
else
{
printf( "Cannot open file.\n");
printf( "Goodbye!\n");
}
break;
default:
printf( "Sorry, that isn't a valid choice.\n\n");
}
}
}
while (q=1);
return 0;
}Also I attached the words_emancip.txt file, just in case you want to run it. Last edited by mmmm_strawberries; Apr 3rd, 2005 at 1:00 PM. |
|
|
|
|
|
#4 |
|
Newbie
|
Ok I thought maybe I messed up by putting
token = strtok(Buffer, delimiters); strcpy( token, strtok(Buffer, delimiters) |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4
![]() |
Sorry I don't know exactly what you want, but I modied your code a bit to make it shorter.
This will store 10 words in a 2D character array and output them aswell #include <stdio.h>
#include <string.h>
int main( )
{
FILE *myfile = NULL; //file pointer
int fileselect; //holds the choice
char filename[30]; //holds the file name
char words[10][20]; //array to hold the words
int i; //for the loop
while(1)
{
printf( "\nSelect the number of the text file to read\n");
printf( "1 - words_emancip\n");
printf( "2 - words_flatland\n");
printf( "3 - words_macbeth\n");
printf( "4 - words_prince\n");
printf( "5 - words_purloin\n");
printf("6 - Quit\n");
fflush( stdin);
scanf( "%d", &fileselect);
printf( "\n");
switch(fileselect)
{
case 1: strcpy(filename, "words_emancip.txt");
break;
case 2: strcpy(filename, "words_flatland.txt");
break;
case 3: strcpy(filename, "words_macbeth.txt");
break;
case 4: strcpy(filename, "words_prince");
break;
case 5: strcpy(filename, "words_purloin");
break;
case 6: printf("Goodbye\n");
exit(1);
break;
default: printf("Incorrect choice, goodbye\n");
exit(1);
break;
}
myfile=fopen(filename, "r");
if(myfile==NULL)
{
printf("File not found\n");
continue;
}
for(i=0; i<10; i++)
{
fscanf(myfile, "%s ", &words[i]);
printf("%s\n",words[i]);
}
fclose(myfile);
}
return 0;
}
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 Last edited by Benoit; Apr 3rd, 2005 at 2:16 PM. |
|
|
|
|
|
#6 |
|
Newbie
|
Thanks, that is a lot cleaner looking. But what I need to ultimately do is store all of the words. Well here's the instructions:
1) Prompt the user to select a text file to read. The file selected must be one provided by the instructor.
2) Validate the user’s selection. Repeat the previous step until a valid selection is entered.
3) Open the selected file for text mode reads.
4) Read a series of words from a text file and save them in an array of strings.
a) Declare and initialize two parallel arrays of at least 5000 elements: one array of strings, each of which will hold a word up to 20 chars long, and one array of int’s which will hold word counts.
b) Read all the words from the file as follows:
i) Read the next word from the file into a temporary string. Each line in the text file will contain exactly one word with no leading or trailing blanks or punctuation.
ii) Increment a count of all words read from the file.
iii) Convert all characters in the word to lower case. Remove the newline char from the end of each word.
iv) Search the array for a copy of the word that may have already been read and placed in the array.
v) If the word is not already in the array, add it to the array and set the corresponding word count in the integer array to one.
vi) If the word is already in the array, increment the corresponding word count in the integer array
vii) If the array is full, increment a count of excess words and discard the word.
5) Close the file.
6) Display to the user the total number of words that were read from the file.
7) Write the contents of the array of strings and the array of word counts to a new text file named “output.txt”.
a) Open the file for text mode writes
b) Write each word and its word count on a separate line of the output file, separated by a comma. Write only the words that were read from the file. Do not write any empty strings to the file.
c) When all words and word counts have been written, close the output file.
8) Display the contents of the first 10 and last 10 words in the array of strings and corresponding word counts to the user.
9) Inform the user that the file processing is complete.Maybe that will help. So what I need to do is store every word and a count of how many times it appears in a parallel array, that way I can sort them later and print the first and last 10 again. |
|
|
|
|
|
#7 |
|
Newbie
|
Here's my professor's sample output.
Select a file by number from this list: 1 - words_emancip.txt 2 - words_flatland.txt 3 - words_macbeth.txt 4 - words_prince.txt 5 - words_purloin.txt Your choice? (1-5) 1 Results for file words_emancip.txt: Total words read from the file = 626 Words in array = 242, Excess words = 0 Writing word list to file named output.txt ... First 10 words in the unsorted array: Word # 0 = the , Count = 57 Word # 1 = emancipation , Count = 1 Word # 2 = proclamation , Count = 5 Word # 3 = by , Count = 7 Word # 4 = president , Count = 3 Word # 5 = of , Count = 38 Word # 6 = united , Count = 13 Word # 7 = states , Count = 20 Word # 8 = america , Count = 1 Word # 9 = a , Count = 8 Last 10 words in the unsorted array: Word # 232 = constitution , Count = 1 Word # 233 = necessity , Count = 1 Word # 234 = invoke , Count = 1 Word # 235 = considerate , Count = 1 Word # 236 = judgment , Count = 1 Word # 237 = mankind , Count = 1 Word # 238 = gracious , Count = 1 Word # 239 = favor , Count = 1 Word # 240 = almighty , Count = 1 Word # 241 = god , Count = 1 Starting to sort ...Sort complete. Writing sorted word list to file named sorted.txt ... First 10 words in the sorted array: Word # 0 = the , Count = 57 Word # 1 = of , Count = 38 Word # 2 = and , Count = 37 Word # 3 = states , Count = 20 Word # 4 = in , Count = 15 Word # 5 = united , Count = 13 Word # 6 = to , Count = 10 Word # 7 = that , Count = 9 Word # 8 = st , Count = 9 Word # 9 = be , Count = 9 Last 10 words in the sorted array: Word # 232 = constitution , Count = 1 Word # 233 = necessity , Count = 1 Word # 234 = invoke , Count = 1 Word # 235 = considerate , Count = 1 Word # 236 = judgment , Count = 1 Word # 237 = mankind , Count = 1 Word # 238 = gracious , Count = 1 Word # 239 = favor , Count = 1 Word # 240 = almighty , Count = 1 Word # 241 = god , Count = 1 Read another file? (y/n) n Press any key to continue |
|
|
|
|
|
#8 |
|
Newbie
|
Ok new question. Disregard previous questions. How do I get it to print the last 10 words of the file? And it won't even give me the first 10 words of files 2-5. It only works for words_emancip.txt
Last edited by mmmm_strawberries; Apr 3rd, 2005 at 3:39 PM. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Apr 2005
Posts: 22
Rep Power: 0
![]() |
last ten words of a file? I would read all the words and create a list with the following structor
struct member { char word[80]; int place; struct word *next; } then count them and print the last ten.If you want more info on how to use this pm. You can even use this to put them in alphabetical order. Last edited by Daniel_kd; Apr 3rd, 2005 at 6:51 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|