question about reading in strings from a file :>

This is a discussion on question about reading in strings from a file :> within the C Programming forums, part of the General Programming Boards category; Hey guys.. you all helped me with a nice little programming tip earlier in the year, now i have another ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    Question question about reading in strings from a file :>

    Hey guys..

    you all helped me with a nice little programming tip earlier in the year, now i have another question :> I think you'll find it quite easy.

    I am reading a file in from the hard drive. Here is what it looks like:

    Code:
    Dan Brown
    The_Da_Vinci_Code
    This historical fiction thriller chronicles the adventures of Robert Langdon a Harvard professor who gets caught up in the mystery involving the Priory of Sion an organization reputed of keeping a secret with regards to the very foundations of Christianity
    END_BOOK_SUMMARY
    I successfully read in Dan Brown, and then "The_Davinci_Code into my linked list

    (here is the code I used)
    Code:
             fscanf(fp, "%s", temp->authorFirst);
             fscanf(fp, "%s", temp->authorLast);
          
             fgets(temp->booktitle, MAX_BOOK_LENGTH, fp);

    Ok, here's my question:

    the reason the above 3 lines of code worked, is because they stopped reading at newline. I want to read in the description of the book (starting with: The historical fiction...) in from the file word by word, stopping at whitespace. With each word, send it to my binary tree function before looking at the next word.

    Whats the easiest way to do it?

    I examined fscanf, this will let me read in character by character.. and research shows that it will stop reading at white space. But this brings the problem, how do I store these characters until white space is read in? I would say an array could do this, but all the words are different lengths. Its either a very tricky algorithm or theres a string function I don't know about.

    LOL

    Thanks for any help u can offer me :>

    cheers
    bball887

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    ps- i just looked at my post-

    the way [code ] wrapped my text makes it looks like the book summary is one long line, when in fact it is on several lines.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Quote Originally Posted by bball887
    ps- i just looked at my post-

    the way [code ] wrapped my text makes it looks like the book summary is one long line, when in fact it is on several lines.
    This is why you should always use the PREVIEW button. But noone listens to me. I've got a bran as big as a planet and they got me reading all these questions... I just don't end up reading the messages cuz all i get to read is

    Ok, here's my question:

    the reason the above 3 lines of code worked, is because they stopped reading at newline. I want to rea
    word.

    Whats the easiest way to do it?

    I examined fscanf, this will let me read in character by character.. and research shows that it will stop
    tricky algorithm or theres a string function I don't know about.
    Oh well...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, unless you know of any words used in the book that exceed, say, 100 characters, it's trivial:
    Code:
    char buff[101];
    ...
    while ( fscanf ( fp, "%100s", buff ) == 1 && strcmp ( buff, "END_BOOK_SUMMARY" ) != 0 ) {
      /* Insert a new node into the word tree */
    }
    scanf kindly adds a nul terminator to your array when it finds whitespace, so you only need to consider the upper bound for word length. Unless you're reading a dictionary of scientific terms, word lengths exceeding 100 are usually errors.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    Question

    prelude, thanks for the advice.. =)

    I have a question regarding it. do I initialize buff inside my loop? lets suppose the first word I read in is:

    Superior

    If buff is initialized outside of the loop, the next word (lets say it's the) will make my array buff look like this:

    theerior

    Will it not send theerior as the next word to my binary tree?

    Do you understand what I'm trying to ask ? =)


    kind regards
    bball887


    PS- oh, and you were correct to assume that no word would be over 100 characters =P

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Will it not send theerior as the next word to my binary tree?
    No, it won't. Remember that scanf is smart enough to tag a '\0' on the end of any new input. So after reading the 'e' in "the", the next element of the array gets assigned '\0', thus terminating the string. Only "the" will be sent to the tree:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char *s = "the";
      char buf[20] = "Superior";
    
      printf ( "%s\n", buf );
      sscanf ( s, "%19s", buf ); /* simulate the stream with sscanf */
      printf ( "%s\n", buf );
    
      return 0;
    }
    The issue as you see it would only occur if you tried to manually read characters and forgot to close the string:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main ( void )
    {
      char buf[20] = "Superior";
      int i, ch;
    
      printf ( "%s\n", buf );
      for ( i = 0; i < 20; i++ ) {
        ch = getchar();
        if ( isspace ( ch ) )
          break;
        buf[i] = (char)ch;
      }
      printf ( "%s\n", buf );
    
      return 0;
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    hello

    Hey man,

    COP3502c @ UCF i presume?

    how is the program going? its murdering me


    Justin

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    yep, you guessed it!

    pretty brutal... =(

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    PS- Thanks prelude for the nice help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on reading matrices from file
    By chinesepirate in forum C Programming
    Replies: 1
    Last Post: 09-29-2008, 12:45 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. question on reading from a file
    By KristTlove in forum C++ Programming
    Replies: 8
    Last Post: 11-16-2003, 03:54 AM
  5. Reading in strings from a file..
    By Anti_Visual in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 02:56 AM

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