按getchar和putchar打印多行 学习C语言,使用微软Visual C++编写和测试代码。
下面的程序使用C from text(第1.5.1节)通过putchar()和getchar()将其输入复制到输出:按getchar和putchar打印多行 学习C语言,使用微软Visual C++编写和测试代码。,c,input,output,getchar,putchar,C,Input,Output,Getchar,Putchar,下面的程序使用C from text(第1.5.1节)通过putchar()和getchar()将其输入复制到输出: #包括 内部主(空) {int c; 而((c=getchar())!=EOF) 普查尔(c); 返回0;} 程序每次按ENTER键都会打印键盘输入的字符。因此,在打印前我只能输入一行。在打印之前,我找不到通过键盘输入多行文本的方法 有没有办法让这个程序从键盘输入和输出多行文字 对不起,如果这是一个基本的无知的问题 提前感谢您的关注和感谢。在这里,只要您键入“+”并按enter
#包括
内部主(空)
{int c;
而((c=getchar())!=EOF)
普查尔(c);
返回0;}
程序每次按ENTER键都会打印键盘输入的字符。因此,在打印前我只能输入一行。在打印之前,我找不到通过键盘输入多行文本的方法
有没有办法让这个程序从键盘输入和输出多行文字
对不起,如果这是一个基本的无知的问题
提前感谢您的关注和感谢。在这里,只要您键入“+”并按enter键,您输入的所有数据都将打印出来。您可以将阵列的大小增加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;
}
#包括
内部主(空)
{int c='\0';
char-ch[100];
int i=0;
而(c!=EOF){
c=getchar();
ch[i]=c;
i++;
如果(c=='+'){
对于(int j=0;j一些巧妙地使用指针算法来做您想做的事情:
#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;
}
一切都是鹦鹉学舌:)
我用过,和。一定要查找它们,因为它们是非常有用的函数(推荐使用fgets
获取用户输入).您是否可以使用GetKeyState功能在按enter键时检查SHIFT键是否被按下?也就是说,您可以使用SHIFT/enter键输入多行,然后使用普通的enter键发送整个内容。例如:
#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);
}
}
#包括
内部主(空)
{int c;
while(true){
c=getChar();
if(c==EOF&&GetKeyState(VK\u LSHIFT){
putchar(“\n”);
继续;
如果(c==EOF)中断,则为else;
否则{
普查尔(c);
}
}
使用setbuffer()
使stdout
完全缓冲(直到缓冲区的大小)
#包括
#定义bufsize8192
#定义第3行
字符buf[BUFSIZE];
内部主(空)
{int c;
int行=0;
setbuffer(标准尺寸、基本尺寸、尺寸(基本尺寸));
而((c=getchar())!=EOF){
行+=(c=='\n');
普查尔(c);
如果(行==行){
fflush(stdout);
直线=0;
}}
返回0;}
使用fgets读取一行输入,然后使用strcat连接输入行。在windows上,您可以通过按alt+10输入新行字符。输出的计时?您何时在Linux中运行该程序?似乎我应该使用Linux而不是windows进行编程。是的,这是在Linux下运行的,但我看不出您为什么不能在Windows下运行此操作。我没有使用任何特殊标题或自定义操作系统行为:)但除此之外,我建议使用Linux for C编程,因为它最流行的编译器GCC支持C99标准。VC++只实现C90,没有计划支持任何新的C标准。当然,这是个人观点,但你的假设是正确的,Linux使编程变得更容易,而不仅仅是C。@user2593692 no problem,很高兴我能帮上忙:)
$ ./a.out
adas
asdasdsa
adsa
adas
asdasdsa
adsa
#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);
}
}
#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;}