I can't read your code. Please use code tags.
It's kind of hard to see what the loops do without proper indentation (code tags fix this problem) so I can't quite see what's exactly happening in the program. One thing:
fgets(line,LINE_BUF,fp);
while (!feof(fp)) {
Len=strlen(line) - 1;
if ( (newNode=malloc(sizeof(ListNode))) == NULL)
{
fprintf(stderr,"\nListInsert: Memory Allocation failed! Aborting.\n");
break;
}
current = head;
previous = NULL;
/* Search to find where in insert new list node */
while (current != NULL )
{
previous = current;
current = current->next;
}
if ( Len > 0) {
strncpy(newNode->word,line,Len);
}
newNode->next = current;
fgets(line,LINE_BUF,fp);
Why do you have 2 fgets() statements? A properly-written file reading function should only need 1.
Another thing: is your file encoding messed up between platforms? Often operating systems store newlines and other charecters differently, which can confuse functions like fgets() and cause problems.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
fgets(line,LINE_BUF,fp);
while (!feof(fp)) {
The behavior you see is probably because the above is not the correct way to code that loop. Here is the correct, and more efficient, way. Not how eof() function is not needed.
while( fgets(line,LINE_BUF,fp) != NULL)
{
// blabla
}
You did not post the declaration of ListNode, but I assume memberword is an array of characters and not a pointer. Your program might be more efficient if you make word a pointer and allocate the required length. Sure, strncpy() will help minimize buffer overflows, but if the source string is longer than the amount of space allocated for the destination string, the result destination string will not be NULL terminated, and that will make other standard string operations (such as strlen) unpredictable.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
MS-Windows fgets() appends a '\n' at the end of the line, which you must remove.
while (fgets( // blabla ) )
{
if( line[strlen(line)-1] == '\n')
line[strlen(line)-1] = 0;
}
If the above doesn't fix the problem, look at the file with Notepad.exe and see if those characters are there. If they are, then the problem is in the data file and not your program.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
MS-Windows fgets() appends a '\n' at the end of the line, which you must remove.
while (fgets( // blabla ) )
{
if( line[strlen(line)-1] == '\n')
line[strlen(line)-1] = 0;
}
I wouldn't say itappends it, but rather leaves it there if in fact it is there. And with very long strings, it seems a waste to calculate the length twice. (I mentioned this to a seasoned programmer in a slightly different light once long ago, so that is why I mention it again here. That problem involved nested loops and strlen and severe performance issues, which is why I feel it is worthy of mention -- it's not a good habit to perpetuate.)
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
I wouldn't say it appends it, but rather leaves it there if in fact it is there. And with very long strings, it seems a waste to calculate the length twice. (I mentioned this to a seasoned programmer in a slightly different light once long ago, so that is why I mention it again here. That problem involved nested loops and strlen and severe performance issues, which is why I feel it is worthy of mention -- it's not a good habit to perpetuate.)
I agree -- in actual programs I calculate the length only once and probably should have shown that method here too.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I agree -- in actual programs I calculate the length only once and probably should have shown that method here too.
Out of curiosity, since you were around for my DevShed thread , what is it? On Cprog some of us had a similar debate once , so I'm just wondering which one you prefer? The strlen to an index, the strlen to a pointer, the strchr , the strrchr , the <a href="http://www.daniweb.com/techtalkforums/post101350-5.html">strcspn</a> , or the strtok , other?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Since '\n' is always the last character in the buffer there is no point using those functions -- they are a waste of time and will do nothing more than slow down the program if used frequently. My opinion is that it is a misuse of those functions.
strlen() to an index or pointer are one in the same -- I prefer index method as in the example I posted
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
> Since '\n' is always the last character in the buffer there is no point using those functions
No it isn't.
If the line is longer than the buffer, then there will be no \n in there at all, so just trashing the character before the \0 is wrong.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
>>But does it really matter which one is faster?
Yes it might, depending on how often it is used and whether it is a real-time program or not. How many programs have you used that are sluggish and show to respond -- one reason is for careless programming and programmers not paying attention to efficiency.
It would appear from your comments that you have never written a real-time program where you have to sequeeze every last nano-second you can out of the program. No reflection on you but what you stated is a common belief about program speed/efficiency versus program maintainability. Sometimes ease of maintainance issues have to be sacrificed for program efficiency.
>>I would use a wrapper function or write my own version of fgets to suit my application.
Yes, that is the best solution if there are a lot of fgets()'s scannered throughout the program.
> Since '\n' is always the last character in the buffer there is no point using those functions
No it isn't.
If the line is longer than the buffer, then there will be no \n in there at all, so just trashing the character before the \0 is wrong.
you are of course correct but I made no mention of that. If it exists it will always always be the last byte just before the null terminator.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I have been researching for some days about this problem with the '\n'
left behind.
This have been posted:
>>I would use a wrapper function or write my own version of fgets to suit my application.
Now I'm intrigued by it. Could anyone post a little example of re-writting the fgets function?.
From Ravalon I heard for first time about wrapper but I'm not sure I follow what it means.
Until I found this forum all that I did was to trash the last character
before '\0' like Ancient Dragon initially posted. Now I'm looking for a better way that I can understand.
Thank you guys, for your guidance.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
to write a wrapper means to write your own function that will eventually call some standard library function
Now, instead of calling fgets() call myfgets()
char* myfgets(char *buf, size_t bufsize, FILE* fp)
{
chr* ptr = NULL;
// validate parameters
if( buf != NULL && bufsize > 0 && fp != NULL)
{
ptr = fgets(buf,bufsize,fp);
if(ptr != NULL)
{
size_t len = strlen(buf);
if( buf[len-1] == '\n')
buf[len-1] = 0;
}
}
return ptr;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Not to be nitpicky, but...
chr* ptr = NULL; Something doesn't look right. :D
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Since '\n' is always the last character in the buffer there is no point using those functions -- they are a waste of time and will do nothing more than slow down the program if used frequently. My opinion is that it is a misuse of those functions.
strlen() to an index or pointer are one in the same -- I prefer index method as in the example I posted
While that might seem to be the "obvious" case, it is not necessarily true.On Cprog some of us had a similar debate once ,Case in point: a reply to my post in which strrchr was the fastest.
Assumptions about library implementations may be unreliable.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Now I'm intrigued by it. Could anyone post a little example of re-writting the fgets function?
There are links in User Input: Strings and Numbers [C] about 2/3 of the way down, called "Read a Line of Text from the User" and "Safe Version of gets()", for more examples to look at.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314