From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e89e4fc8b5c5f198 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: get_line Date: 1998/06/06 Message-ID: #1/1 X-Deja-AN: 360182606 References: <6lbg7h$nnv$1@goanna.cs.rmit.edu.au> Organization: Network Intensive Newsgroups: comp.lang.ada Date: 1998-06-06T00:00:00+00:00 List-Id: Dale Stanbrough writes: > Well, no, not quite always. If there are the exact number of characters > in the input as will fit in the string, then it will automatically skip > the end of line. The code above would then skip the following line as > well. I don't think the statement above is correct. If there are the same number of characters in the input line as the input buffer, Get_Line will NOT do an end-of-line automatically. The next Get_Line will in fact return 0 as the value of last. (It's the 2nd call the Get_Line that consumes the end-of-line.) The test to make sure all of the text on the line has been consumed is Last < Input_Buffer'Last which means "you have read all the text on the current line." To use this predicate, you'll have to make your buffer size 1 more than the advertised maximum. These issues and techniques are described in John Barnes' book. > The solution is to read the characters in char by char. It is > unfortunate that Get_Line has such a weak postcondition. I don't think a character-by-character read is required. Yes, it probably seems weird that you have to allocate an extra character in your input buffer. But you just have to do that in order to make the test if Last < Line'Last then ...; Here is a little program that illustrates how to properly read a maximum-sized input line. Note that when the input is equal to the max length (5), the line terminator is consumed, as expected. (This is because the actual buffer has length 6.) Lines larger than the advertised max length are flagged as too long, and the rest of the input line is consumed. with Ada.Text_IO; use Ada.Text_IO; procedure Test_Get_Line is Max_Length : constant Positive := 5; Line : String (1 .. Max_Length + 1); Last : Natural; begin loop Put ("Ready: "); Get_Line (Line, Last); exit when Last = 0; if Last < Line'Last then Put_Line ("Entry '" & Line (1 .. Last) & "' is OK."); else Skip_Line; Put_Line ("Entry was too long."); end if; end loop; end Test_Get_Line; $ test_get_line Ready: 1 Entry '1' is OK. Ready: 12 Entry '12' is OK. Ready: 123 Entry '123' is OK. Ready: 1234 Entry '1234' is OK. Ready: 12345 Entry '12345' is OK. Ready: 123456 Entry was too long. Ready: 1234567 Entry was too long. Ready: 123456 Entry was too long. Ready: 12345 Entry '12345' is OK. Ready: 1234 Entry '1234' is OK. Ready: 123 Entry '123' is OK. Ready: 12 Entry '12' is OK. Ready: 1 Entry '1' is OK. Ready: