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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8d5bda3619cce0f8 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!r41g2000prr.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: C getchar() functionality in Ada Date: Mon, 16 Feb 2009 15:46:22 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <4999ce31$0$90266$14726298@news.sunsite.dk> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1234827982 23074 127.0.0.1 (16 Feb 2009 23:46:22 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 16 Feb 2009 23:46:22 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: r41g2000prr.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:4634 Date: 2009-02-16T15:46:22-08:00 List-Id: On Feb 16, 12:36 pm, Thomas Locke wrote: > Hey all, > > I've started redoing all the K&R C examples in Ada, and I've already hit > a wall that I'm not able to get around. > > I have this C program: http://pastebin.com/fbc6bec5 > > It's very simple, but still I fail to mimick it in Ada. > > The C program happily accepts everything I throw at it, and it responds > with the expected values, whereas my Ada version(s) either fail at > linefeeds, throw End_Error exceptions at me, or spits out too many > linefeeds! > > My current Ada code looks like this:http://pastebin.com/f519f7e37 > > This version works with input from keyboard and when I pipe some data > into it like this: $ echo "FooBar" | ito > > It craps out on files ($ cat SomeFile | ito), where it ignores linefeeds > and throws End_Error exceptions at me when the last character in the > file is a linefeed I'd be surprised if the program actually ignores *all* linefeeds--- that would indicate something is wrong with your compiler. But you're probably seeing that blank lines get ignored---is that correct? Ada doesn't think of Text_IO files just as streams of characters (with linefeed being one of the characters); files are structured in terms of lines and pages, without specifying just what form the line separators and page separators look like. Here, End_Of_Line tests to see if the input file is at the end of the line, but it does *not* "eat" the line separator. You'll have to use Skip_Line for that. Also, Get will skip over any line separators that it's facing. So if there are two or more line separators in a row, End_Of_Line will not swallow any of them, and then Get will swallow all of them. Furthermore, if there are multiple line separators followed by the end- of-file, End_Of_File will not return True because there are additional "empty" lines, but then Get will eat all the line separators and then raise End_Error because there is no character to read. I think that with this information you can probably figure out how to make it work, as close as possible. But keep in mind that you are *still* not going to be reading and writing individual characters the same as in C. On Windows, the line separator is often CR LF (two characters), and there's really no way in Ada (using Text_IO) to treat that as two separate characters. If you need that, you'll have to go outside the standard Ada library. Plus there's no telling what your implementation will do if it sees CR or LF by itself, or LF CR, or other mutant combinations of those control characters. In any event, the program you're writing probably has no practical use. If you really want to read individual characters and treat control characters just like all other characters, you probably don't want to use Text_IO. If that isn't what you want, then you likely don't want to read individual characters. I realize that this is not a practical program that you're trying to mimic, but rather a example to demonstrate some point. Hope this helps, -- Adam