Thread: read words from file

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

    read words from file

    So say I have the following file:

    Apple
    Adam
    Bank
    Carlos
    Orange
    People
    Queen
    Zebra


    I wan't to create a getWord method that takes in a FILE* f

    The example above is just a small file, but I will be using this method for a large file.. what's the best way to do this? so when I do getWord(f) I got Apple then the next time I do getWord(f) I get Adam and so on

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Use fgets()

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I was thinking to use fgets as well, but I don't know how long the string will be in each line

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    If you're afraid one of the "words" will be huge, you can have the program move forward until it finds the newline to get the length of the line, then malloc()s that much memory, and then goes back and calls fgets. Or you can start at a certain size and fgets that much and then realloc() if there's more, and fgets again, etc.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay could you help me how to code this then?

    is it like this:

    Code:
    char result[200];
    return fgets(result, 200, file);
    but using this code I am going to have to read from the disk everytime I want to get the word right? this is slow..

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Quote Originally Posted by -EquinoX- View Post
    okay could you help me how to code this then?

    is it like this:

    Code:
    char result[200];
    return fgets(result, 200, file);
    but using this code I am going to have to read from the disk everytime I want to get the word right? this is slow..
    The operating system would most likely buffer the file for you.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay so does my code looks good?

    Code:
    char* getword (FILE* fp)
    {
      char result[132];
      return fgets(result, 200, fp);
    }
    Last edited by -EquinoX-; 05-01-2008 at 07:07 PM.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    okay so does my code looks good?
    Horrible - you are telling lies to fgets
    as a result it will try to write 200 chars into buffer able to store only 132

    You also return pointer to the local variable

    Code:
    char* getword (FILE* fp, char* result, size_t max_len)
    {
      return fgets(result, max_len, fp);
    }
    
    char result[132];
    char* ret = getword (fp,result, sizeof result);
    and because in this case your getword is just a wrapper for fgets - you can get rid of it... Or add some functionality - for example get rid of the trailing \n character as shown in the FAG
    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
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay I made a mistake in returning the local variable so I guess I should do a malloc and then return that? right

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by -EquinoX- View Post
    okay I made a mistake in returning the local variable so I guess I should do a malloc and then return that? right
    you can use malloc - just do not forget to free the memory after you finished with it
    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

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay gotcha!

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by -EquinoX- View Post
    okay I made a mistake in returning the local variable so I guess I should do a malloc and then return that? right
    You can, but as vart suggested, a local variable at the level above is much better - unless you need to malloc some memory ANYWAYS later on - then it makes sense to do all of that in that function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Read words and numbers from a file
    By atari400 in forum C Programming
    Replies: 5
    Last Post: 11-04-2004, 04:55 PM