Tuesday, September 13, 2011

LPTHW - Exercise 16

In the previous exercise we read the contents of a file. In this exercise we are going to read and write files.

We will open a file in the 'w' mode and then truncate it. Then we accept three lines of input from the user, and append them to the file.



Extra Credit:

In the next example, we comment most of the lines of the solution and also reuse the prompt by using replacement characters.



We need not have called truncate on the fp, since opening a file in 'w' mode automatically truncates it.

2 comments:

  1. Can you please do the study drill 3 of this exercise ?

    ReplyDelete
    Replies
    1. target.write (line1 + "\n" + line2 + "\n" + line3)

      Delete