scanf或getchar一次10000次,然后程序在c中停止

时间:2015-04-22 04:53:14

标签: c scanf getchar

#include "stdio.h"

int main() {
    int ch,i;
     for (i = 0; i < 10000; ++i) {
        scanf(" %c", &ch);
        putchar(ch);
     }
     return 0;
}

如果你把字符小于1000但是如果你一次放置10000个字符就可以了,那么程序就会停止。我可能认为这是因为stdin缓冲区。 我该如何解决这个问题?

这是更具体的来源。

#include "stdio.h"
#include "stdlib.h"

int main() {
    int i,n;
    char *p, ch;

    scanf("%d", &n);
    p = (char*) malloc(sizeof(char) *n);

    if (p == NULL) {
        return -1;
    }

    for (i = 0; i < n; ++i) {
        scanf(" %c", &ch);
        putchar(ch);
    }
    return 0;
}

Abode源是样本。 - 将n(字数统计为读取)然后放长字符串。程序停止或等待完成任务。

输入示例 1。

  

2000   lsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdk lsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdkalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalssdafsdfsdfdd

这个不起作用。

但是下面的工作正在进行中。 2。

  

1000   jsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdkalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalssdafsdfsdfddkdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalsdklsakdjldjsalkfjalssdafsdfsdfdd1

1 个答案:

答案 0 :(得分:0)

您的for循环可能如下所示:

for (i = 0; i < n; ++i) {
    scanf(" %c", &p[i]);
    putchar(p[i]);
}

您还可以尝试在代码中刷新stdin输入缓冲区,因为正确添加以下行:

fflush(stdin);

编辑:由于某些系统上存在未定义的行为可能性,因此可以采用以下方法实现替代且更常见的方法:

int ch;

while((ch = getchar()) != EOF && ch != '\n') {

      // ...
}

或者您可以通过调用下面的 setvbuf 函数来正确更改流缓冲区大小:

int setvbuf (FILE* stream, char* buffer, int mode, size_t size);