------------------------------------------------------------------------------
MC logo
Reading Lines
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Hello, World! (Plain C)] [Average C I/O] [Printf Demo] [Problems With C I/O] [Looping On Read] [Convert To Upper Case] [Indent and Blank Remover] [Reading Lines] [Safer Line Reading] [Printing C++ Strings]
lines1.c
/*
 * This uses gets to read input one line at a time.  
 * Gets is risky since it has no way to check if the 
 * input line overflows the the buffer.  This also 
 * demonstrates a one stupid printf() trick.
 */
#include <stdio.h>

#define LINWID 55

/*
 * Print a LINWID line, with +'s on the ends.
 */
void prline()
{
        int n = LINWID;
        putchar('+');
        while(n--) putchar('-');
        putchar('+');
        putchar('\n');
}

main()
{
        char buf[150];

        prline();
        while(gets(buf))
                // Print the line at LINWID with | | around it.
                printf("|%-*s|\n", LINWID, buf);
        prline();
}