忽略C程序输入中的#comments

时间:2015-05-21 04:37:11

标签: c comments

我正在尝试编写一个程序,它将从文本文件中读取,然后输出最小值,最大值和平均值。我遇到的麻烦是忽略了以标签开头的文本文件中的注释。到目前为止,这是我的工作代码。有人可以帮忙吗?

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

int main( void )
{
  char ch, filename[20];
  FILE *lun;
  int num, min, max, sum, count, first;

  printf("Please enter the  name of file to load:");
  scanf ("%s", filename);
  lun=fopen(filename, "r");
  if ( lun != NULL)
    {
      for ( sum= count= first= 0; fscanf( lun, "%d", &num ) == 1; sum += num, ++count )
        if ( !first ) { min= max= num; first= 1; }
        else if ( num > max ) max= num;
        else if ( num < min ) min= num;
      fclose( lun );

      printf( " Minimum value: %d\n Maximum value: %d\n Average value: %lf\n",
              min, max, sum / (double) count );
    }
  else
    printf( "Unable to read file.\n" );

  return 0;
}

1 个答案:

答案 0 :(得分:2)

读取行中的数据(使用fgets())。 如果该行包含#,请将'#'替换为'\0'来终止该字符串。然后扫描行中的数字。 另请参阅How to use sscanf() in loops?

不要忘记检查文件是否已打开。

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

int main(void)
{
    char filename[20];

    printf("Please enter the name of file to load: ");
    if (scanf("%19s", filename) != 1)
    {
        fprintf(stderr, "Failed to read file name\n");
        return 1;
    }
    FILE *lun = fopen(filename, "r");
    if (lun == NULL)
    {
        fprintf(stderr, "Failed to open file %s for reading\n", filename);
        return 1;
    }
    char line[4096];
    int min = 0;  // Avoid compilation warnings (may be used uninitialized)
    int max = 0;  // Ditto
    int sum = 0;
    int count = 0;
    while (fgets(line, sizeof(line), lun) != NULL)
    {
        char *hash = strchr(line, '#');
        if (hash != NULL)
            *hash = '\0';
        int pos;
        int num;
        int off = 0;
        while (sscanf(line + off, "%d%n", &num, &pos) == 1)
        {
            if (count == 0)
                min = max = num;
            if (num > max)
                max = num;
            if (num < min)
                min = num;
            sum += num;
            count++;
            off += pos;  // Skip through line
        }
    }
    fclose(lun);

    printf("Minimum value: %d\nMaximum value: %d\nAverage value: %lf\n",
              min, max, sum / (double)count);
    return 0;
}

如果您的编译器不支持C99或更高版本,则必须将变量声明移动到块的开头(紧跟在{之后)。

处理双打并不是更难:

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

int main(void)
{
    char filename[20];

    printf("Please enter the name of file to load: ");
    if (scanf("%19s", filename) != 1)
    {
        fprintf(stderr, "Failed to read file name\n");
        return 1;
    }
    FILE *lun = fopen(filename, "r");
    if (lun == NULL)
    {
        fprintf(stderr, "Failed to open file %s for reading\n", filename);
        return 1;
    }
    char line[4096];
    double min = 0.0;   // Avoids 'used when uninitialized' warnings
    double max = 0.0;   // Avoids 'used when uninitialized' warnings
    double sum = 0;
    int count = 0;
    while (fgets(line, sizeof(line), lun) != NULL)
    {
        char *hash = strchr(line, '#');
        if (hash != NULL)
            *hash = '\0';
        int pos;
        double num;
        int off = 0;
        while (sscanf(line + off, "%lf%n", &num, &pos) == 1)
        {
            if (count == 0)
                min = max = num;
            if (num > max)
                max = num;
            if (num < min)
                min = num;
            sum += num;
            count++;
            off += pos;  // Skip through line
        }
    }
    fclose(lun);

    printf("Minimum value: %f\nMaximum value: %f\nAverage value: %f\n",
              min, max, sum / count);
    return 0;
}