some help on fgets()

This is a discussion on some help on fgets() within the C Programming forums, part of the General Programming Boards category; hi guys, im trying to read a text file and get one word at a time. e.g : i want ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    some help on fgets()

    hi guys, im trying to read a text file and get one word at a time.

    e.g : i want to eat ice- cream, i'd like to eat chocolate and maybe a lolipop!

    will display
    i
    want
    to
    eat
    ice
    cream
    i'd
    like
    to
    eat
    chocolate
    and
    maybe
    a
    lolipop

    i figure i would nid to use fget to store the characters in a buffer and scan each line for every character. while i understand how to print the text in a while, i have no idea how to scan each character to achieve my results. can someone explain to me please?

    Code:
    while(fgets(entry, sizeof(entry) , myfile))
    {
    		printf("%s",entry);
    }

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    hmm maybe strtok can do the trick

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    i got it ! strtoken works

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    fscanf works better for reading words.
    Code:
    while ( fscanf(myfile, "%s", entry) == 1 )
    {
        printf("%s\n", entry);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 09:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 01:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21