Quote:
but, what's the problem with fgets(), its a pretty good function, and if the problem is removing \n when possible, you can make this:
Code:
char *mygets (char *buf, int size) {
char *p;
fgets(buf,size,stdin);
p = strchr(buf,'\n');
if(p) *p = '\0';
return buf;
}
Because if this was the function and it was done more than once, and you entered a string that exceeded the buffer size, you would get something like this.
Code:
Enter text ("0" to exit): here is my text
text = "here is m"
Enter text ("0" to exit): text = "y text"
Enter text ("0" to exit): 0
text = "0"
-----
Quote:
1: No offense, but is this a joke?
No, no joke. If we took the code from the FAQ and made a function out of it, we'd have code similar to the above. Then to solve that problem, we might respond, "flush the input buffer". And I'm sure we could search and find some code that looks like this.
Code:
while ((ch = getchar()) != '\n' && ch != EOF);
And we'd add this to the 'exceeded buffer size' condition. But being a function, maybe we'd return a value that tells us if an error occurred. That is essentially what I posted, with long-winded and standard-speak comments.
Quote:
If you just want to see it in the FAQ, you could have PM'd Hammer.
If I thought it was a complete solution, I might have. But it was what is was. A start, with questions and a solicitation for suggestions. (And I'm pleased with the thoughtful responses so far.)
Quote:
2: Since it's your own function, why retain the meaningless standard library return values? You could return the number of bytes read on success and EOF for an error or EOF.
Quote:
>/*
>* Consume the 'extra' characters remaining in the stdin.
>* This is done by reading the character and discarding
>* it (by not placing it in 'buffer' which is already full).
>*/
I'm not sure I like this part. [...] assume that any further input would be needed and may not be modified [...]
Quote:
Also, the newline is a valuable error checking tool, if you remove it and still return a null pointer, the user has no clue whether a recoverable error occurred, or everything is going to die. vVv's suggestion of returning a more useful value is a good idea, perhaps a set of constants that mark end of file, read error, success, empty line, leftover input, etc...
Quote:
What if I wanted to use your mygets to write a function that reads arbitrarily long lines (which happens to be a common occurance, I've done it many times)? [...]
Then I might need to do another iteration of this function! Issues like these are what I wanted to hear. Thanks.
"Trivial" as this may be, I don't work with hosted systems 'for real', so I have not handled such "common occurances" "dozens of times". And I am meaning no offense either. I'm just trying to learn the language better. And others stand a chance to learn something new as well.
Quote:
Note that checking for a null argument may be overkill, you have to consider how much your users have to be protected from themselves at the cost of performance.
I considered making the null check an assert, but if the function stops the system to wait for user input, I wouldn't think performance was a big issue.
Quote:
I definately don't like the idea of a line being considered invalid, and ignored if a newline character is not returned within the fgets() call. (this has probably been covered, but this is my 2-cents worth). A problem would is very visibible if you redirect input into your program from a file. The last line is simply ingored. Example usage:
c:\>davesprog.exe <file_with_three_lines.txt
Enter text ("0" to exit): text = "line 1"
Enter text ("0" to exit): text = "line 2"
Enter text ("0" to exit):
And another glaring problem I overlooked. Thanks for pointing it out.
Quote:
don't be put off by negative comments
Where? :)