C noob needs help with reading words from a file and printing them with a word count
Hello, this is my first post here. I know literally no one else that can help me.
I'm trying to read the words from a file and printing them out along with the number of the word. For example - if the file said "Hello my name is Meelad" the output would look like this
This is my code :
My output is far different, it's posting special characters and I don't know why because, as I said before, am a C noob. If this is breaking any of the rules, let me know.
int make_dictionary(char *fname)
FILE *fp; /* File open* */
fp = fopen(fname, "r");
char c; /* Variable for getc() */
char phrase; /* String holder */
int wordcount = 1; /* Word counter */
int i; /* Character counter */
if( fp == NULL )
printf( "Oops! Could not open the file %s\n", fname );
for(i = 0; (c = getc(fp)) != EOF; i++)
if( c == EOF )
break; /* End of file! */
else if(isalpha(c) != 0)
phrase[i] = c;
printf(" %d\n", wordcount);
else if(isalpha(c) == 0)
phrase[i] = '\0';
i = 0;
Edit: This is a homework assignment, but this is just a small portion of it that I can't get to work correctly.
Edit2: I'm also not sure about what to do when there's multiple non alphabetic characters in a row.
Edit3: I think I may have found out the answer. Sorry if this is spam now, I can't find out how to delete the post :\