Advice requested, Code makes sense to me, not compiler

This is a discussion on Advice requested, Code makes sense to me, not compiler within the C Programming forums, part of the General Programming Boards category; Why don't you try to debug by checking the value of the string after the read, before the newline char ...

  1. #46
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Why don't you try to debug by checking the value of the string after the read, before the newline char strip and then afterwards?
    The code should only strip the newline char and nothing else.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #47
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    latest full code
    Attached Files Attached Files

  3. #48
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    did that, it was the same word but it knocked 2 characters off the front of the next line

  4. #49
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    either way, I'm calling it a night for the time being, i will work on it in the morning and update accordingly. Thankyou for the great help and advice, its much appreciated, and I hope by the end of this thread to be able to post the full code that actually works

  5. #50
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    getword() returns a local array of characters, so all bets are off on what's coming back to you at that point. You need to either:

    1. change the return of getword to a char *, malloc the required memory, strcpy the word into it, return that, use it, and free the malloc'd memory, or

    2. change the function signature to pass a buffer, into which you copy the word

    Also, so you don't become a help vampire, you need to learn how to use gdb.

    Code:
    /* Option 1 */
    char *getword(FILE *fp, int max)
    {
        //...assuming at this point buffer is a null-terminated string
        char *word_to_return = malloc(strlen(buffer) + 1);
        strcpy(word_to_return, buffer);
        return word_to_return;
    }
    
    /* Option 2 */
    const char *getword(FILE *fp, char *buffer_for_return, int buffer_size, int max)
    {
        // ...as above
        strncpy(buffer_for_return, buffer, buffer_size);
        buffer_for_return[buffer_size - 1] = '\0';
        return buffer_for_return;
    }
    Last edited by rags_to_riches; 01-05-2008 at 08:38 PM. Reason: Additional info

  6. #51
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    Changed things a bit, instead of doing the malloc you suggested i pass a char[] pointer to the getword function so it writes directly into the array, but the same problem still occurs (cut off words) and is directly caused by the
    Code:
    //if (buffer[size - 1] == '\n') buffer[size - 1] = '\0';
    line.

    the problem is still the trailing newlines, i know replacing the \n with \0 should work, but it doesnt.

    output without cutoffline (above)
    Code:
    A bored
     distance
    
    --------------------
     A bored
     distance
     check
     above
     the chubby
     help
    
     never
     the distance
     check
     's
     The help
     ban
     alongside-of
     a hot
     air
    output with cutoff line
    Code:
     hatear
    --------------------
     frontrotesque
     's dhate
     bagentlet
    NB obviously these outputs are completly different wordsets

    full code enclosed
    Attached Files Attached Files

  7. #52
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Guess: fgets is reading end-of-line as two characters: \r\n (this happens when you read a file written in Windows on a *nix system, for instance). The \n is being removed, but the \r isn't (the \r moves the "print head" back to the start of the line). This would explain, for instance, why your 's, which is listed at the end of a line, is printed at the start of it.

    Remove \n and then remove \r using the same trick, and see what happens.

  8. #53
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45

    Talking

    Bingo, problem solved, thankyou, i cant believe i missed that. *hangs head in shame*

    Code:
    A adventurous band
    --------------------
     A adventurous band improve above the curious head
     nearly the band improve's
     The head gather at a chubby interest
    next step is fixing the code that decided if "a" or "an" is needed. Will post when fixed

  9. #54
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    code fixed
    Code:
    An annoying apparatus
    --------------------
     An annoying apparatus deliver at the gigantic glove
     generally the apparatus deliver's
     The glove fumble around a bad burst
    thankyou all so much for your help, it is very much appreciated.

    enclosed is the final code (not properly documented, but if you want that, pm me at some point later)
    Attached Files Attached Files

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  2. my code is messed up says the compiler
    By 0927 in forum C++ Programming
    Replies: 11
    Last Post: 01-30-2002, 12:59 PM
  3. Replies: 4
    Last Post: 01-15-2002, 11:04 PM
  4. Replies: 3
    Last Post: 11-04-2001, 02:53 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM

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