【问题标题】:Total digits and digits per line in C?C中每行的总位数和位数?
【发布时间】:2014-01-19 15:24:17
【问题描述】:

你能帮我找出我在计算每行位数然后将其与总数进行比较时的错误吗?这是我计算每行位数的程序:

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

int main()
{    
    int pc, c, NumbersPerLine= 0, NumbersTotal=0, line= 1; 
    FILE *inputFile;

    if(!(inputFile=fopen("C:\\Test\\Test.txt","r")))
    {
        printf("the file does not exist\n");
        return 0;
    }

    for (pc='\n', c=fgetc(inputFile);c!=EOF;pc=c,c=fgetc(inputFile)) 
        { 
            if (isdigit(c))
            {
                NumbersPerLine++;
            }
            else 
                if (c=='\n') 
                {  
                 printf("%d %d\n", line++, NumbersPerLine); 
                 NumbersPerLine= 0;
                } 
        }     
        if (pc!='\n') printf("%d %d %d\n", line, NumbersPerLine); 
        fclose(inputFile);
}

但是现在我也必须添加文件中的总位数,我所做的只是逐行计算它们,然后将它们相加而不是全部计算。 我试图得到这样的结果:

1 (that's the number of the line) 5 (the number of digits per line) 18 (total digits) 
2 5 18 
3 4 18 
4 1 18 
5 1 18 
6 1 18 
7 1 18 

我试着把 NumbersTotal++;在 NumbersPerLine++; 之后,但我得到的只是这个:

"1 5 5 
2 5 10 
3 4 14 
4 1 15 
5 1 16 
6 1 17 
7 1 18"

我也试过在'for'之前使用do-while

c=fgetc(inputFile);        
do        
{        
NumbersTotal++;        
}while(isdigit(c));        

但在满足该条件后,程序结束并且不会继续使用“for”。你能帮帮我吗?

【问题讨论】:

  • "per line" -> 使用fgets() 而不是fgetc()。而且那个 for 循环太可怕了。
  • 当出现“文件不存在”的错误消息时,没有什么比这更烦人的了。试试char *path="C:..."; if(!(inputFile=fopen(path, "r"))) { perror(path);return EXIT_FAILURE;}

标签: c numbers digits


【解决方案1】:

如果您通读该文件一次,则必须重新打开该文件或回到开头重新阅读。但是,只读取一次文件并在该循环期间跟踪两个统计信息会更干净。

【讨论】:

    【解决方案2】:

    [编辑] 感谢@BLUEPIXY 编辑,可以更清楚地看到 OP 希望打印每行的最终总数。

    进行 2 次传球。第一次找到总数,第二次通过打印。

    也可以使用

    int NumbersTotal = 0;
    while ((c = fgetc(inputFile)) != EOF) {
      if (isdigit(c)) NumbersTotal++;
    }
    
    rewind(inputFile);
    
    // The rest same as OP with matching format specifiers and arguments.
    for (pc='\n', c=fgetc(inputFile);c!=EOF;pc=c,c=fgetc(inputFile)) { 
      if (isdigit(c)) {
        NumbersPerLine++;
      }
      else if (c=='\n') {  
        printf("%d %d %d\n", line++, NumbersPerLine, NumbersTotal);
        NumbersPerLine= 0;
        }     
      }
    }
    
    if (pc!='\n') {
      printf("%d %d %d\n", line, NumbersPerLine, NumbersTotal); 
    }
    fclose(inputFile);
    

    for() 循环语法可以使用一些重构。示例:

    for (pc ='\n'; (c=fgetc(inputFile)) != EOF;  pc=c) { 
    

    【讨论】:

      【解决方案3】:

      当读取文件两次成为负担时,将中间结果写入文件的策略。

      #include <stdio.h>
      #include <string.h>
      #include <ctype.h>
      
      int main(void){
          int ch, NumbersPerLine= 0, NumbersTotal = 0, line = 0; 
          FILE *fin, *fp;
          char line_buff[32], *fname = "result.tmp";
      
          fin = fopen("data.txt", "r");//Check omitted
          fp  = fopen(fname, "w+");
          for(;;){
              ch = fgetc(fin);
              if(isdigit(ch)){
                  ++NumbersPerLine;
              } else if(ch == '\n' || ch == EOF){
                  NumbersTotal += NumbersPerLine;
                  fprintf(fp, "%d %d %%d\n", ++line, NumbersPerLine); 
                  NumbersPerLine= 0;
                  if(ch == EOF) break;
              }
          }
          fclose(fin);
          fflush(fp);
          rewind(fp);
          while(fgets(line_buff, sizeof(line_buff), fp)){
              printf(line_buff, NumbersTotal);
          }
          fclose(fp);
          remove(fname);
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-08-20
        • 2014-12-20
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 2019-08-03
        • 2021-10-03
        • 2016-05-17
        相关资源
        最近更新 更多