|
Newbie
Join Date: Feb 2005
Posts: 10
Rep Power: 0 
|
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.
|