Reading problem

This is a discussion on Reading problem within the C Programming forums, part of the General Programming Boards category; Code: #include "bst_inter.h" void bst_letter_fill ( tree_t *T ) { FILE *in; char read; bst_item data; int check; in = ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Reading problem

    Code:
    #include "bst_inter.h"
    
    void bst_letter_fill ( tree_t *T )
    {
      FILE *in;
      char read;
      bst_item data;
      int check;
    
      in = fopen("fill.txt", "r");
    
      while (fscanf(in, "%c ", &read) != EOF);
      {
        data.code = read;
        data.key = read;
        bst_insert(T, &data);
      }
      fclose (in);
    }
    This read statement is supposed to fill up a previously declared and initiallized binary search tree with the contents of fill.txt,

    E A R I O T N S L C U D P M H G B F Y W K V X Z J Q

    but is only going through the while loop once, recording Q. Any ideas why that is happening, and not recording the entire text file? It should be some simple, stupid reading error. Any help is appreciated

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You have an empty while loop.
    Code:
    while (fscanf(in, "%c ", &read) != EOF);
    It may be better to check for a successful read to enter the body of the loop.
    Code:
    while ( fscanf(in, "%c ", &read) == 1 )
    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.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Thanks for pointing out the semi-colon, it loads now

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    New problem, this one is kinda crazy. The program reads correctly up to the value G, then letters start getting mixed up.

    Code:
    #include "bst_inter.h"
    
    void bst_letter_fill ( tree_t *T )
    {
      FILE *in1, *in2;
      bst_item data;
    
      in1 = fopen("english.txt", "r");
      in2 = fopen("cipher.txt", "r");
    
        while (fscanf(in1, "%c", &data.key) != EOF && fscanf(in2, "%c", &data.code) != EOF)
          bst_insert(T, &data);
    
        fclose (in1);
        fclose (in2);
    }
    where data includes a code character and a key character.

    english.txt: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    cipher.txt: TSWEPFARUZMBCIYLJXHQOKNGVD

    The resulting tree is:

    Code: A Key: T
    Code: B Key: S
    Code: C Key: W
    Code: D Key: E
    Code: E Key: P
    Code: F Key: F
    Code: X Key: G
    Code: H Key: R
    Code: I Key: U
    Code: J Key: Z
    Code: K Key: M
    Code: W Key: N
    Code: P Key: L
    Code: O Key: Y
    Code: T Key: Q
    Code: R Key: X
    Code: Y Key: V

    Any ideas why it would not read (or print?) the whole text file, and why the letters only start getting rearranged after F? Any help is greatly appreciated!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Put in some useful prinf statements in your insertion function, in your loop, etc, printing out what data is read, and the like. This is a common and useful method for finding out where your problems occur.


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

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Thanks. printf. I fixed it.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    FYI...

    fscanf() is a powerful waste of time, and memory for reading a character. It's designed for reading complex input.

    fgetc() is designed for reading characters and will do a better job faster, easier, and with less chance for mistakes.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem reading numbers in a txt file
    By nimamc in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 02:35 PM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Problem reading from a file..
    By Candelier in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 12:42 AM
  4. Replies: 6
    Last Post: 05-12-2005, 03:39 AM

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