Start of file probs

This is a discussion on Start of file probs within the C Programming forums, part of the General Programming Boards category; Sorry to keep pestering people but I'm getting an error I can't seem to bypass. Code: char process (void) { ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    137

    Start of file probs

    Sorry to keep pestering people but I'm getting an error I can't seem to bypass.

    Code:
    char process (void) {       
       char read;   
       while (read=fgetc(fptr)!=EOF) {
       	if ((read >= 65) && (read <= 90)) {
          	read = convert (read);};//converts to lower class if needed
          insert_node (&tree,read);
    		return read;
    }
    This isn't the full code but a function I've written. The idea is to basically read a text file ( containing something like "hello" ) a character at a time and return the value it got.
    My problem is that I only get one value returned whatever the contents of the file. ASCII 1, the start of file ( equivalent to EOF but at the start ) character. Any ideas why its only returning one and not just reading them all.
    Thanks
    http://uk.geocities.com/ca_chorltonkids

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > while (read=fgetc(fptr)!=EOF) {

    Generally it's considered a GoodThing(TM) to write it this way:

    while( (read=fgetc(fptr)) != EOF ) {

    Also, you may not want to use 'read' for a variable name. There are low level read / write functions called, oddly enough, 'read' and 'write'.

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

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    If your intention was:
    while( (read=fgetc(fptr)) != EOF )

    then
    >> char read;
    Would be wrong. fgetc returns an int type. You need to use this to compare with EOF. Otherwise if you happen to read in a char with value 0xff, it will equal EOF and break unexpectedly. Declare it as an int type and if it's not EOF, treat it as a char.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    I can't believe I spent so long racking my brain because I messed up my brackets. I'm sure it wont be the last time though, its annoying how you can read something so many times and yet not see it.

    I have a function that creates a node in a binary tree:
    Code:
    void insert_node ( struct node **root, char data ) {
        if ( *root == NULL ) {
            // a new leaf is needed
            struct node *newnode = (struct node*) malloc( sizeof( struct node ) );
            newnode->data = data;
            newnode->left = NULL;
            newnode->right= NULL;
            *root = newnode;
        } else {
            // decide which way to go
            if ( data < (*root)->data ) {
                insert_node( &(*root)->left, data );
            } else {
                insert_node( &(*root)->right, data );
            }
        }
    }
    ( Thanks all who ironed out my errors with it )
    If I call it myself multiple times it works fine:
    insert_node (&tree,'c')
    insert_node (&tree,'d')
    insert_node (&tree,'e')

    Inside my loop however it will only create one node, despite how many times its called:
    Code:
    while( (read=fgetc(fptr)) != EOF )  {
       	if ((read >= 65) && (read <= 90)) {
          	read = convert (read);};
          insert_node (&tree,read);
    Do you think this be caused by the value of read not being updated properly or maybe it's my insersion function that doesn't like being called multiple times?
    Thanks
    http://uk.geocities.com/ca_chorltonkids

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    I didn't know you could do that.
    http://uk.geocities.com/ca_chorltonkids

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by crag2804
    I didn't know you could do that.
    Welcome to the poor man's debugger:

    printf("%c has been added to the list\n", data );

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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