can someone tell me how to do this , say I have
apple
orange
california
days
baloon
hotel
car
jack
how do I read words by words using fread?
can someone tell me how to do this , say I have
apple
orange
california
days
baloon
hotel
car
jack
how do I read words by words using fread?
you should use fgets...how do I read words by words using fread?
fread reads exact number of bytes - it cannot stop on the new line...
using fread however you can read all the file into buffer - and use strchr to parse the buffer into words
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
so how do I do this?
I heard that fread also reads the words faster as it doesn't have a disk access I/o
if it does not access disk, how it can read something?I heard that fread also reads the words faster as it doesn't have a disk access I/o
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I mean it just access the disk once.. am I right?
is it true that using fread to read words is faster? I need to write a method called getword that will help me to bring a word in each line and return that word
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
here's my method:
however it goes through an infinite loop and it nevers return NULL, why is this?Code:char* getword (FILE* fp) { char result[100]; fgets(result, 100, fp); if (result != NULL) return StringConstruct(result); else{ return NULL; } }
and by the way string construct is just a method that I create to malloc and do a strcpy to a new memory location.. so I don't return a local variable
Last edited by -EquinoX-; 05-04-2008 at 10:21 PM.
I think you mean to check the return value of fgets against NULL.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
okay I changed the code to this one below but still it doesn't works:
it's still an infinite loopCode:char* getword (FILE* fp) { char result[100]; if(fgets(result, 100, fp) != NULL) return StringConstruct(strtok(result, "\n")); else{ return NULL; } }
Last edited by -EquinoX-; 05-04-2008 at 10:37 PM.
Maybe the issue is in StringConstruct().
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> char *StringConstruct(char* word) { char *s = malloc(strlen(word) + 1); if ( s ) { strcpy(s, word); } return s; } char* getword (FILE* fp) { char result[50]; if ( fgets(result, sizeof(result), fp) != NULL ) { int len = strlen(result)-1; if ( result[len] == '\n' ) result[len] = 0; return StringConstruct(result); } else return NULL; } int main() { static const char filename[] = "file.txt"; FILE *file = fopen(filename, "r"); if ( file ) { char *word; while ( (word = getword(file)) ) { puts(word); free(word); } fclose(file); } return 0; } /* apple orange california days baloon hotel car jack */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
what's the issue in StringConstruct it looks good.. my code looks like above.. the thing is that when I try to traverse through a short list it works but for a long list it goes over and over again
Then perhaps the issue is in neither getword nor StringConstruct.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*