reading file word by word

This is a discussion on reading file word by word within the C Programming forums, part of the General Programming Boards category; Hi i'm trying to read a text file into a linked list storing each word in a different node in ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    reading file word by word

    Hi

    i'm trying to read a text file into a linked list storing each word in a different node in the list.

    i don't know how to tell it to stop reading when it gets to the end of the word (a space, newline or punctuation mark).

    this is the code i have so far:

    Code:
    /* opening and reading from a file */
    void readword()
    {
    	int n;
    	char c[50];
          file *text;
          
          text = fopen("filename", "r");
       
    /* here i need to tell it to stop reading when it gets to the end of the word */
       
    {
    	n = fread(c, 1, 50, text);
          c[n]='\n';
    }
    
          fclose(text);
    return 0;
    
    }
    thanks a lot any help will be greatly appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, use a function meant for text. Consider fscanf, or perhaps the dynamic duo of fread and sscanf. Alternately, if you're bored, or just feel like having some fun, use fgetc.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main (void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          int ch, word = 0;
          while ( (ch = fgetc(file)) != EOF )
          {
             if ( isspace(ch) || ispunct(ch) )
             {
                if ( word )
                {
                   word = 0;
                   putchar('\n');
                }
             }
             else
             {
                word = 1;
                putchar(ch);
             }
          }
          fclose(file);
       }
       return 0;
    }
    
    /* file.txt
    All work and no play makes Jack a dull boy.
    The quick brown fox jumps over the lazy dog.
    */
    
    /* my output
    All
    work
    and
    no
    play
    makes
    Jack
    a
    dull
    boy
    The
    quick
    brown
    fox
    jumps
    over
    the
    lazy
    dog
    */
    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. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  5. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 06:13 PM

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