Mail Archives: djgpp/1997/03/12/20:53:46
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 -