【问题标题】:print multiple lines by getchar and putchar通过 getchar 和 putchar 打印多行
【发布时间】:2013-07-17 01:44:03
【问题描述】:

我是一名学习 C 编程语言并使用 Microsoft Visual C++ 编写和测试代码的初学者。

下面的 C 语言程序从文本(第 1.5.1 节)通过 putchar() 和 getchar() 将其输入复制到其输出:

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

程序每次按回车键都会打印键盘输入的字符,结果只能输入一行再打印。我找不到在打印前通过键盘输入多行文本的方法。

有什么方法可以让这个程序从键盘输入输出多行文本?

对不起,如果这是一个基本且无知的问题。

提前感谢您的关注和感谢。

【问题讨论】:

  • 使用 fgets 读入一行输入,然后使用 strcat 连接输入行。
  • 在windows上可以按alt+10输入换行符。
  • 输出的时间是什么时候?

标签: c input output getchar putchar


【解决方案1】:

巧妙地使用指针算法来做你想做的事:

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

这就是我使用它的方式:

$ ./a.out 
adas
asdasdsa
adsa

adas
asdasdsa
adsa

一切都是鹦鹉学舌的:)

我用过fgetsstrcpystrlen。一定要查找它们,因为它们是非常有用的功能(fgets 是接受用户输入的推荐方式)。

【讨论】:

  • 你是在Linux下运行程序的吗?看来我应该用Linux而不是windows来编程。
  • 是的,它是在 Linux 下运行的,但我看不出为什么你不能在 Windows 下运行它。我没有使用任何特殊的头文件或自定义操作系统行为 :) 但除此之外,我建议使用 Linux 进行 C 编程,因为它最流行的编译器 GCC 支持 C99 标准。 VC++ 只实现了 C90,并且没有计划支持任何更新的 C 标准。这当然是个人观点,但您的假设是正确的,因为 Linux 使编程变得更容易,而不仅仅是 C。
  • @user2593692 没问题,很高兴我能帮上忙 :)
【解决方案2】:

在这里,只要您键入“+”并按 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;


    }

您可以在 '+' 字符或任何您想表示打印操作的字符上设置条件,这样该字符就不会存储在数组中(我现在还没有在 '+' 上设置任何此类条件)

【讨论】:

    【解决方案3】:

    使用setbuffer() 使stdout 完全缓冲(达到缓冲区的大小)。

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

    【讨论】:

      【解决方案4】:

      您可以使用 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);
           }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-02
        • 1970-01-01
        • 1970-01-01
        • 2013-10-22
        相关资源
        最近更新 更多