Your code has some issues:
Most of the times, you don't want to do the check feof(fp)
at the top of a loop when reading a file. It is almost guaranteed that if you do so, you will read the last line of the file twice. This is because feof()
returns true only if an earlier read attempt resulted in "end of file". Better method is to check if fgets()
returns NULL
:
while (fgets(str, LINE_SIZE, fp) != NULL) {
/* do your processing */
}
if (ferror(fp)) { /* there was an error reading some data */ }
Your isspace()
call should cast *str
to unsigned char
if it is not of that type:
while (isspace((unsigned char)(*str))) {
To use isspace()
, you should #include <ctype.h>
.
You need to increment str
. Your expression str+1
evaluates str+1
, discards its value, and does nothing. So, you need:
++str;
But you don't want to increment str
: you need that for the next fgets()
call. So, you should copy it to a temporary pointer:
char *tmp = str;
while (isspace((unsigned char)(*tmp))) {
++tmp;
}
Making all the changes above, your loop becomes:
while (fgets(str, LINE_SIZE, fp) != NULL) {
char *tmp = str;
while (isspace((unsigned char)(*tmp)))
++tmp;
for (cp = tmp; cp = strtok(cp, "AEIOUaeiou\n"); cp = NULL) {
puts(cp);
}
}
(You have an extra closing }
, most likely a copy-paste error.)