reading word from file

This is a discussion on reading word from file within the C Programming forums, part of the General Programming Boards category; is there a standard function to read exactly one word from a file (a word means to me: everything between ...

  1. #1
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90

    reading word from file

    is there a standard function to read exactly one word from a file (a word means to me: everything between spaces or tabs or cr's or linefeeds)

    stormbringer

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Not really

    You could use
    fscanf( fp, "%s", word );

    But you don't really know how big the word is (it could be 1MB long for some badly formatted file)

    The only safe way to do this is to use fgets to read a line, then write some code to look at that line to decide where the words are in that line.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    You can write you own little function to accomplish reading one word at a time by using getc.

    Put getc in a while loop and have it look for spaces, cr(s) and new lines.

    Use putc to put your individual characters in an array, then decide what to do with the word that you read in.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by gabulldog
    Use putc to put your individual characters in an array, then decide what to do with the word that you read in.
    What? Um, no. First off, putc doesn't put anything into an array. It puts it into a file (using fputc), or the standard output device. Now I suppose it is possible to redirect stdout to a file, but still, that doesn't get it into an array.

    Unless of course, you redirect standard out to a file, or perhaps another application, and then used that to read the input, or just read the file, and then put it into an array.

    But really, is there really any point? Why on earth would you need a function to put some data in an array?
    Code:
    c = fgetc( myfile );
    if( !isspace( c ) )
    {
        array[counter++] = c;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    You're right about putc. My bust.

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, 12:44 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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