Re: [cc65] Get user input?

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2004-09-26 22:10:21
On Sun, Sep 26, 2004 at 11:26:25AM -0400, Raj Wurttemberg wrote:
> One little oddity about the above input method... It appears to also capture
> the carriage return character.

fgets is defined so that it includes the newline character if there is one. If
there is no newline character, this mean that you are either at the end of the
file, or the buffer was too short to read the whole line.

> Is there a way to get it to not do that?

No. You will have to remove it manually as you did, but

>  -  crchar = strlen(buf)-1;
>  -  buf[crchar]=0;

this is dangerous. The input may not contain anything at all, or no newline
character, in which cases your code will fail and possibly do strange things.
My standard solution (used heavily within the compiler itself) is (from
src/cc65/codeopt.c):

        /* Remove trailing white space including the line terminator */
        B = Buf;
        Len = strlen (B);
        while (Len > 0 && IsSpace (B[Len-1])) {
            --Len;
        }
        B[Len] = '\0';

This does remove all whitespace at the end of the string, including a newline.
The nice thing about it is that it does also work with MS-DOS files (having a
cr/lf line termination) correctly, even when they're read under Unix. If you
just want to remove a newline, use

        len = strlen (buf);
        if (len > 0 && buf[len-1] == '\n') {
            buf[--len] = '\0';
        }

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Sun Sep 26 22:10:26 2004

This archive was generated by hypermail 2.1.8 : 2004-09-26 22:10:35 CEST