1. Re: Dropping last character (Was: (No Subject))

>If you can give me an example where gets()
>fails (drops the last line), I'd be happy to investigate.
>
>gets() is supposed to return the whole line including '\n'
>if there is one. The last line in a file might not have
>a '\n', so gets() will return all the characters on that line,
>but there won't be a '\n' on the end.
>
>Some editors do funny things when the last line in
>your file does not end with '\n'.  ed.ex will
>add a '\n' to your line. ee.ex seems to
>drop the last character on the line that's missing the \n.
>Are you reading David? I tried:
>
>junk.ex:
>puts(1, "line1\nline2")   -- missing final \n
>
>ex junk >junk.dat
>
>ex ee junk.dat
>
>and found the '2' in line2 was missing.

I can see what's happening here. It's normal to see something like:

while 1 do
    junk = gets(fn)
    if not atom(junk) then
        line = line & {junk[1..length(junk)-1]}
    else
        exit
    end if
end while

Notice how the last character is chopped off? It's an assumed \n. In the
case of a last line without a \n, as Rob just showed, the entire line is
read, but there isn't any \n to read, so it isn't added. The code should
do something like this (but would be slower, seeing as it would only do
anything on the last line, but we don't know where the last line is, so
we always need to do the check):

while 1 do
    junk = gets(fn)
    if not atom(junk) then
        if junk[length(junk)] != '\n' then
            junk = junk & '\n'  -- Add \n for the next check
        end if
        line = line & {junk[1..length(junk)-1]}
    else
        exit
    end if
end while

This will prevent the last character from being chopped off if there
isn't a \n at the end of file.

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu