Print multiple lines by getchar and putchar

I am starting to learn the C programming language and using Microsoft Visual C ++ to write and test code.

Below the program in text C (section 1.5.1), copy your input to your output via putchar () and getchar ():

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

The program prints the characters entered by the keyboard each time you press ENTER. As a result, I can enter only one line before printing. I cannot find a way to enter multi-line text from the keyboard before typing.

Is there a way and how to allow this program to input and output multi-line text from the keyboard?

Sorry if this is a basic and uninformed question.

Appreciate your attention and thanks.

+4
source share
4 answers

Some clever use of pointer arithmetic to accomplish 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 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 here is how I use it:

 $ ./a.out adas asdasdsa adsa adas asdasdsa adsa 

Everything is built back :)

I used fgets , strcpy and strlen . Take a look at them as they are very useful features (and fgets are the recommended way of user input).

+1
source

Here, as soon as you type "+" and press, enter all entered data until it is printed. You can increase the size of the array over 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 the condition in a '+' char or any other character that you would like to represent for the print action so that this character is not stored in the array (I did not apply such a condition to '+' right now)

0
source

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

 #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;} 
0
source

Can I use the GetKeyState function to check if the SHIFT key is held down by pressing the enter key? That is, you could enter several lines using SHIFT / ENTER and send it all using the usual ENTER key. Sort of:

 #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); } } 
-one
source

Source: https://habr.com/ru/post/1492027/


All Articles