Thread: reading words using fread

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    reading 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?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    how do I read words by words using fread?
    you should use fgets...
    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

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so how do I do this?

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I heard that fread also reads the words faster as it doesn't have a disk access I/o

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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?
    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

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I mean it just access the disk once.. am I right?

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by -EquinoX- View Post
    I mean it just access the disk once.. am I right?
    It depends on the implementation... but because it does not checks the read symbo;s as fgets - to decide if the end of line riched - it maybe faster... I still do not undestand how you plan to determine the end of word without previosly reading the 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

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    here's my method:

    Code:
    char* getword (FILE* fp)
    { 
      char result[100];
      fgets(result, 100, fp);
      if (result != NULL)
       return StringConstruct(result);
      else{
        return NULL;
      }
    }
    however it goes through an infinite loop and it nevers return NULL, why is this?

    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.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay I changed the code to this one below but still it doesn't works:

    Code:
    char* getword (FILE* fp)
    {
      char result[100];
      if(fgets(result, 100, fp) != NULL)
        return StringConstruct(strtok(result, "\n"));
      else{
        return NULL;
      }
    }
    it's still an infinite loop
    Last edited by -EquinoX-; 05-04-2008 at 10:37 PM.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  2. using fread on stdin
    By nadroj in forum C Programming
    Replies: 29
    Last Post: 10-23-2008, 02:03 PM
  3. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  4. Sorting a file with a lot of words.
    By samus250 in forum C Programming
    Replies: 28
    Last Post: 04-27-2008, 01:36 PM
  5. Need help in debugging this code
    By jaggesh in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 08:47 AM