1

I am going through the Kernighan and Ritchie's The C Programming Language, Second Edition and I am having a problem understanding the nature of the assignment in Exercise 1-16. I've read it over many times but cannot figure out exactly what they are asking of me; forgive me but English is not my first language.

The exercise description is:

Revise the main routine of the longest-line program so that it will correctly print the length of arbitrarily long input lines and as much as possible of the text.

Does this mean that the program should no longer be constrained by the MAXLINE string limitation when it comes to the length of the longest line or that it wants you to print every line alone with their lengths? If it's the latter, does it want you to print each line after it is entered along with its length or just the lengths?

The two options I've come up with are quite different so I would really appreciate some advice.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
Yusif_Nurizade
  • 133
  • 1
  • 12
  • 2
    It wants you to print the length of any arbitrary input line. Also, _as much as possible_ of the text. I.e. up to the MAXLINE string limitation. – JohnH Apr 15 '14 at 20:11
  • 1
    It means you should be able to keep track of the length of the longest line even if it is 1 KiB, 2 KiB, 4 KiB, etc, but that you only need to be able to print part of the line (for example, the first 256 characters or so of it). In particular, you should not break a line in the middle simply because it is too long to fit in a buffer. – Jonathan Leffler Apr 15 '14 at 20:11
  • Thank you all for the clarification. I misunderstood the question and ended up trying to get the program to print any line regardless of length. I was having difficulty so I looked up a sample answer and saw they were doing something radically different. – Yusif_Nurizade Apr 16 '14 at 14:30

1 Answers1

2
  1. Does this mean that the program should no longer be constrained by the MAXLINE string limitation?

No, it doesn't. At this point in the book, you're not supposed to know how to handle and store lines with an arbitrary length. Chapter 1 is a very superficial introduction to C, it is not expected that you redesign the whole code.

With that in mind: the exercise is asking you to print as much as you can of the line, i.e., if the line is longer than MAXLINE characters, you just print the first MAXLINE. But you should also print the length of the original line. In other words, you store as much as you can up to MAXLINE characters, but you keep reading after that limit just for the purpose of counting.

To do this, the getline() function must be updated to keep counting the chars until the end of line, even if it reached the limit of the buffer, in which case it counts but it obviously doesn't write the text to the buffer.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • 1
    Note that the assignment asks only for the main routine to be revised which some understand as that the getline and copy functions shouldn't be changed at all. – bonehead Feb 26 '19 at 11:54