4

Im a beginner learning The C Programming language and using Microsoft visual C++ to write and test code.

Below program in C from text(section 1.5.1) copy its input to its output through putchar() and getchar():

#include <stdio.h>
int main(void)
{   int c;
    while ((c = getchar()) != EOF)
         putchar(c);
    return 0;}

The program print characters entered by keyboard every time pressing ENTER.As a result,I can only enter one line before printing. I can't find a way to enter multi-line text by keyboard before printing.

Is there any way and how to let this program input and output multi-line text from keyboard?

Sorry if this is a basic and ignorant question.

Appreciate your attention and thanks in advance.

user2593692
  • 43
  • 1
  • 4

4 Answers4

1

Some clever use of pointer arithmetic to do what you want:

#include <stdio.h>  /* this is for printf and fgets */
#include <string.h> /* this is for strcpy and strlen */
#define SIZE 255 /* using something like SIZE is nicer than just magic numbers */

int main()
{
    char input_buffer[SIZE];        /* this will take user input */
    char output_buffer[SIZE * 4];   /* as we will be storing multiple lines let's make this big enough */

    int offset = 0; /* we will be storing the input at different offsets in the output buffer */

    /* NULL is for error checking, if user enters only a new line, input is terminated */
    while(fgets(input_buffer, SIZE, stdin) != NULL && input_buffer[0] != '\n') 
    {
        strcpy(output_buffer + offset, input_buffer); /* copy input at offset into output */
        offset += strlen(input_buffer);               /* advance the offset by the length of the string */
    }

    printf("%s", output_buffer); /* print our input */

    return 0;
}

And this is how I use it:

$ ./a.out 
adas
asdasdsa
adsa

adas
asdasdsa
adsa

Everything is parroted back :)

I've used fgets, strcpy and strlen. Do look those up as they are very useful functions (and fgets is the recommended way to take user input).

Nobilis
  • 7,310
  • 1
  • 33
  • 67
  • Did you run the program in Linux?Seems that I should use Linux instead of windows for programming. – user2593692 Jul 19 '13 at 00:16
  • Yes, that was run under Linux, but I see no reason why you couldn't run this under Windows. I've not used any special headers or custom OS behaviour :) But otherwise I recommend using Linux for C programming as its most popular compiler GCC supports the C99 standard. VC++ only implements C90 and there no plans to support any newer C standards. And this is a personal opinion of course but your assumption is correct in that Linux makes it easier to do programming, not just for C. – Nobilis Jul 19 '13 at 01:01
  • @user2593692 No problem, glad I was of help :) – Nobilis Jul 19 '13 at 03:43
0

Here as soon as you type '+' and press enter all the data you entered till then is printed. You can increase the size of array more then 100

#include <stdio.h>
    int main(void)
    {   int c='\0';
         char ch[100];
         int i=0;
        while (c != EOF){
          c = getchar();
          ch[i]=c;
      i++;

            if(c=='+'){

            for(int j=0;j<i;j++){
                printf("%c",ch[j]);
            }
        }
    }
        return 0;


    }

You can put a condition on '+' char or whatever character you would like to represent print action so that this character is not stored in the array ( I have not put any such condition on '+' right now)

Sanyam Goel
  • 2,138
  • 22
  • 40
0

Use setbuffer() to make stdout fully buffered (up to the size of the buffer).

#include <stdio.h>
#define BUFSIZE 8192
#define LINES 3
char buf[BUFSIZE];
int main(void)
{   int c;
    int lines = 0;
    setbuffer(stdout, buf, sizeof(buf));
    while ((c = getchar()) != EOF) {
         lines += (c == '\n');
         putchar(c);
         if (lines == LINES) {
              fflush(stdout);
              lines = 0;
         }}
    return 0;}
jxh
  • 69,070
  • 8
  • 110
  • 193
-1

Could you use the GetKeyState function to check if the SHIFT key is held down as you press enter? That was you could enter multiple lines by using SHIFT/ENTER and send the whole thing using the plain ENTER key. Something like:

#include <stdio.h>
int main(void)
{    int c;
     while (true){
         c = getChar();
         if (c == EOF && GetKeyState(VK_LSHIFT) {
              putchar("\n");
              continue;
         else if(c == EOF) break;
         else {
              putchar(c);
     }
 }
Kai Kuspa
  • 25
  • 4