OK so I'm taking a C class to sharpen up my C skills, but I'm having a real hard time with the input methods. First of all a lot of people say don't use gets(), ever. It's dangerous because it doesn't do bounds checking, use fgets() instead. Fine, but the problem with that is that for some reason fgets() stores the newline character in the string. Normally that's probably not what you'd want because now if you store someone's name, for example, and print it in a list or table, it's going to print that newline along with it. So I have no idea what they were thinking there, especially since that's supposed to be the file equivalent of gets(), which does not store the newline character in the string. And it turns out it's a real PITA to actually do all the checking you need to see if you got a string at all, if the length is the same as, less than, or more than the maximum you specified, and then manually figuring out how to manipulate the string, inserting and removing \n and \0 characters to get a proper string. Using %s in scanf()/fscanf() has all kinds of problems too, not the least of which is that it's going to stop when it hits a whitespace character, so you can't do something like just get someone's full name.
Then there's the problem of a lot of these functions leaving newlines and/or garbage in the input buffer, so while it might read something right the first time, the next time you go to read you get some really weird results. And there doesn't seem to be an easy way to clear out that buffer either (which these functions should probably optionally be able to do themselves, but they don't). There's the fflush() function, but it's supported in different ways (including not at all) on different platforms, and I've seen multiple people say never try to use it to flush input, even if the platform claims to support it.
Finally, I'm just specifically getting weird behavior using Xcode with the "Standard Tool" type and any of the ANSI options (trying to use C99) selected. fgetc(), for example, will work OK in one run of the program, then sometimes it will randomly send itself into an endless loop, pouring characters I'm not typing into the input and eventually crashing the app if I don't force-kill it.
The bottom line is all these C input functions just seem like a huge, unholy pile of crap. They're broken, buggy, unsafe, difficult to get what you want out of them when they do work (like, say, a single character typed on the keyboard), and have unpredictable behavior. When they wrote the C99 spec, wouldn't that have been a good place to fix all these things and implement newer, cleaner library functions? Maybe it's just me, but I see a ton of people having the same types of issues online, not just in the Mac/Xcode/GCC implementation, but with all versions of ANSI C.
I guess my question is, what am I missing here? Are there replacement functions I'm not aware of? Am I just crazy? Is it really as bad as it looks to me?
Then there's the problem of a lot of these functions leaving newlines and/or garbage in the input buffer, so while it might read something right the first time, the next time you go to read you get some really weird results. And there doesn't seem to be an easy way to clear out that buffer either (which these functions should probably optionally be able to do themselves, but they don't). There's the fflush() function, but it's supported in different ways (including not at all) on different platforms, and I've seen multiple people say never try to use it to flush input, even if the platform claims to support it.
Finally, I'm just specifically getting weird behavior using Xcode with the "Standard Tool" type and any of the ANSI options (trying to use C99) selected. fgetc(), for example, will work OK in one run of the program, then sometimes it will randomly send itself into an endless loop, pouring characters I'm not typing into the input and eventually crashing the app if I don't force-kill it.
The bottom line is all these C input functions just seem like a huge, unholy pile of crap. They're broken, buggy, unsafe, difficult to get what you want out of them when they do work (like, say, a single character typed on the keyboard), and have unpredictable behavior. When they wrote the C99 spec, wouldn't that have been a good place to fix all these things and implement newer, cleaner library functions? Maybe it's just me, but I see a ton of people having the same types of issues online, not just in the Mac/Xcode/GCC implementation, but with all versions of ANSI C.
I guess my question is, what am I missing here? Are there replacement functions I'm not aware of? Am I just crazy? Is it really as bad as it looks to me?