View Single Post
Old 12-10-2008, 10:45 AM   #4
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,615
Default

Quote:
Originally Posted by Playbahnosh View Post
I gotta get a C book... I feel left out...
Sorry. I guess there's no point in splitting this topic in two now that one of them would be solved... :P

There are dozens of free tutorials, even whole books (tutorials are better to start).

This kind of code isn't difficult so you should get the hang of it after a while. The only special thing here is that instead of taking the trouble to call specific functions to handle files, Data took advantage of the fact that C/C++ handles files and input/output devices in the same way, as "streams". However once opened files would have been interfaced with the same kind of code. The "<" in the command line call told the OS to use one file as standard input stream, instead of the keyboard by default, and the ">" to use another file as standard output stream, instead of the console. So if you for example used the former but not the latter, you should get the intended result, but printed on the screen instead of saved to a file.

Other than that, this code is a good example for beginners wanting to start to learn input/output.

BTW Data, there was another reason to use string.empty() and .clear() instead of == and =, the same reason why they exist at all, since you've shown that you wouldn't need the former if you used the latter instead. Not that the code would be more robust, but that it would be more efficient. I know that's not an issue here, but it's good not to let that get out of sight as a good practice, so that it will come naturally when it be important. I haven't looked inside the string object source code, but I'd bet the implementations of .empty() and .clear() are similar to what I typed, and will likely be inline functions; whereas the overloaded == and = will call functions devised to operate with whole strings so they will include a loop (even when it doesn't iterate more than once).
__________________
Life starts every day anew. Prospects not so good...
Japo is offline                         Send a private message to Japo
Reply With Quote