delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/12/20:53:46

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: DJGPP text editing
Date: Wed, 12 Mar 1997 18:59:55 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 62
Message-ID: <33276DAB.53DC@cs.com>
References: <3325A4E3 DOT 6175 AT juno DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp103.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Ben Shadwick wrote:
> 
> I've been working on a BBS door program in DJGPP, and I was wondering if anyone has any
> suggestions (or info on some programming libraries) about how to make it so that users
> can input a 4-line message (or hit enter on a blank line to end the message early). C is
> a real pain compared to BASIC when it comes to input, output, and strings.

Actually, C is just a more low-level language than BASIC.  It has
a complete and comprehensive set of standard string manipulation
functions, and quite a few more nonstandard ones.  You just have to
know what to use.  :)

Okay, here's how I would write a generic function to retrieve an
arbitrary number of lines from a text stream.  There could probably
be a lot more refinements, but this should do for starters.  Compile
and run the following (tested) example program:

#include <stdio.h>
#include <string.h>

/* Read 'lines' lines of length 'len' from the file indicated by 'fp'.
 * Store the results in 'str', treated as an array of characters of
 * dimensions [len][lines].  Return the number of lines read, and
 * strips carriage returns from the input.
 */
int read_lines( char *str, int lines, int len, FILE *fp )
{
    int i;
    char *p;

    for ( i = 0; i < lines; i++ )
    {
        fgets( str + i * len, len, fp );

        /* strip carriage return from returned line. */
        if ( ( p = strrchr( str + i * len, '\n' ) ) != NULL )
            *p = '\0';

        if ( *(str + i * len) == '\0' )
            return i;
    }
    return i;
}

int main( void )
{
    char input[4][80];
    int i, n;

    n = read_lines( input, 4, 80, stdin );

    for ( i = 0; i < n; i++ )
        puts( input[i] );

    return 0;
}
-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I |        fighteer AT cs DOT com          |
| Descent 2: The Infinite Abyss - The greatest Internet game of all |
| time just got better!  This time, you're going all the way down...|
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019