每次从标准输入读取字符的最佳方法是什么?

时间:2015-04-17 02:13:23

标签: c stdin

所以基本上我正在编写一个C程序,它从标准输入读取字节流,并将字节视为0到255(包括0和255)范围内的无符号整数。程序计算0到255范围内每个值的出现频率。它还接受非负整数作为命令行 论点。此命令行参数给出程序应生成的输出行数n。因此,如果n为16,程序应打印16行输出,显示0到15范围内的字节值发生的频率。
每行应以整数值开头,后跟计数,例如
0发生1014次
1发生1201次
等等。

我尝试每次从stdin读取一个char并检查它是否是“\ n”。但是条件(令牌!=“\ n”)永远不会返回False,并且循环永远不会被破坏 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, const char* argv[]) {
    char token;
    int n;
    if (argc != 2) {
        printf("Error!\n");
        exit(0);
    }

    n = atoi(argv[1]);
    int i;
    int freq[n];

    for(i = 0; i<n; i++) {
        freq[i] = 0;
    }

    int value;
    printf(">");
    token = fgetc(stdin);

    while (token != "\n") {
        printf("here!");
        value = token;
        if (value < n) {
            freq[value] ++;
        }
        token = fgetc(stdin);
    }
    printf("there");

    for(i = 0; i<n; i++) {
        printf("%d  occured %d times\n",i, freq[i]);
    }

    return 1;
}

4 个答案:

答案 0 :(得分:2)

  

我尝试每次从标准输入读取一个字符,并检查它是否&#34; \ n&#34;。但是条件(令牌!=&#34; \ n&#34;)永远不会返回False,循环永远不会被破坏。

那是因为:

while (token != "\n") {

是一个错误。那应该是:

while (token != '\n') {

您的编译器应警告该错误。这是我使用gcc -Wall编译程序时得到的结果:

soc.c: In function ‘main’:
soc.c:25:18: warning: comparison between pointer and integer [enabled by default]
     while (token != "\n") {
                  ^
soc.c:25:18: warning: comparison with string literal results in unspecified behavior [-Waddress]

为了更安全,请使用:

while (token != '\n' && token != EOF ) {

此外,您应该将token使用的类型从char更改为intfgetc()的返回类型为int。如果您的平台使用unsigned char类型,则会遇到捕获EOF的问题,通常为-1

答案 1 :(得分:0)

您可以使用getchar(); -

int c;

while((c=getchar())!=EOF){

}

答案 2 :(得分:0)

以下是发布的代码,附有评论..

// <-- strongly suggest reading the manual for the system functions you use
// so the error on the returned value from fgetc() would not have happened

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, const char* argv[])
{
    char token;   // <-- fgetc and getchar return integer, not character
    int n;

    if (argc != 2)
    {
        printf("Error!\n"); // <-- this should be a 'usage' statement
        exit(0);            // <-- this exit is due to an error, returned value should be a non-zero value to indicate an error occurred
    }

    // implied else, right number of parameters.

    n = atoi(argv[1]);  // <-- check that 'n' is not zero and not negative and <= 255
                        // <-- output an error message and exit of value is not good

    // implied else, parameter valid

    int i;
    int freq[n];

    for(i = 0; i<n; i++)
    {
        freq[i] = 0;
    }

    int value;
    printf(">");


    // <-- suggest modification to while(), so fgetc() embedded in while statement
    token = fgetc(stdin);
    while (token != "\n") { // <-- this is a comparison between pointer and integer, compiler warning
                            // <-- comparions with string literal results in unspecified behavior
        printf("here!");
        // <-- token, after correcting declartion to int eliminates the need for 'value'
        value = token;
        if (value < n) {
            freq[value] ++;
        }
        token = fgetc(stdin);
    }
    printf("there");

    for(i = 0; i<n; i++) {
        printf("%d  occured %d times\n",i, freq[i]);
    }

    return 1;  // <-- this is the 'good' exit, so should return 0
}

答案 3 :(得分:0)

一般来说,您要做的就是完成以下操作:

int ch;

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