Search code examples
ckernighan-and-ritchie

Clarification (not solution) regarding K&R exercises


while going through the first chapter I found a question *to remove all comments from a code" - I really don't know what the author expects, whether I am supposed to use File handling (which in later chapters) or am I supposed to type in the entire file as an input?


Solution

  • You don't need file handling, you can use "getline" like in the later examples in chapter 1 to parse lines. Then if you want to use your code on a file you can pipe the file as input to the executable you have written.

    You could also use "getchar" as in the "wc" example (line, word, and character counting example) given in Section 1.5.4. In that example program there is no file handling, but you can pipe a file the executable just as you would for the usual linux "wc" utility.