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.
Printable View
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.
latest full code
did that, it was the same word but it knocked 2 characters off the front of the next line
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 :D
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;
}
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
line.Code://if (buffer[size - 1] == '\n') buffer[size - 1] = '\0';
the problem is still the trailing newlines, i know replacing the \n with \0 should work, but it doesnt.
output without cutoffline (above)
output with cutoff lineCode:A bored
distance
--------------------
A bored
distance
check
above
the chubby
help
never
the distance
check
's
The help
ban
alongside-of
a hot
air
NB obviously these outputs are completly different wordsetsCode:hatear
--------------------
frontrotesque
's dhate
bagentlet
full code enclosed
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.
Bingo, problem solved, thankyou, i cant believe i missed that. *hangs head in shame*
next step is fixing the code that decided if "a" or "an" is needed. Will post when fixedCode:A adventurous band
--------------------
A adventurous band improve above the curious head
nearly the band improve's
The head gather at a chubby interest
code fixed
thankyou all so much for your help, it is very much appreciated.Code:An annoying apparatus
--------------------
An annoying apparatus deliver at the gigantic glove
generally the apparatus deliver's
The glove fumble around a bad burst
enclosed is the final code (not properly documented, but if you want that, pm me at some point later)