【问题标题】:User input through terminal with input file用户通过带有输入文件的终端输入
【发布时间】:2014-03-06 15:50:24
【问题描述】:

所以我计算了从文件中读取的双精度值的平均值和标准偏差。

我的文件数据每行有1个数字:我的文件中的数据如下

1
2
3
4
5
6
7
8
9
10

我的代码如下:

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

int main(){

    FILE *inputfile= fopen("file.dat.txt", "r");

    if (inputfile == NULL)
    {
        printf("Failed to open text file.\n");
        exit(1);
    }

    double i; 
    double j=1;
    double average; 
    double stdish=0;
    double stdreal=0; 
    double x=0;
    double sum=0;
    double stdfinal;

    while(fscanf(inputfile, "%lf", &i) != EOF){
        x=x+1;
        sum = sum + i;
        j = i*i;
        stdreal +=j;
    }
        average = sum/x;
        stdish = (stdreal/x)-(average*average);
        stdfinal = sqrt(stdish);

    printf("The average is %.4lf\n", average);
    printf("The standard deviation is %.4lf\n", stdfinal);

 fclose(inputfile);
return 0;
}

我正在通过终端运行它。 我的数据文件是file.dat.txt。我想要做的是让用户通过终端输入文本文件,而不是在程序中。

像这样:./sdev &lt; file.dat

我不确定如何在我的程序中实现它...

谢谢!

【问题讨论】:

  • 你从stdin而不是inputfile阅读。

标签: c input terminal


【解决方案1】:

用参数编写主函数。这样您就可以在调用可执行文件时从终端传递文件名。

查看此路径中的示例程序: http://www.astro.umd.edu/~dcr/Courses/ASTR615/intro_C/node11.html

【讨论】:

    【解决方案2】:

    基本上有两种方法可以做到这一点。第一种方法是通过文件重定向通过stdin 接受输入。这是您提出的方法:

    $ ./sdev < file.dat
    

    然后你的程序应该从stdin而不是inputfile读取。您可以更改所有出现的“inputfiletostdin, but you could also makeinputfilea shallow copy to the file handlestdin”:

    int main()
    {    
        FILE *inputfile = stdin;
    
        /* do stuff */
        return 0;
    }
    

    请注意,您不会关闭 stdin。它是由操作系统管理的系统文件句柄。不是NULL

    第二种方法是将文件指定为命令行参数:

    $ ./sdev file.dat
    

    然后,您的程序必须使用接受命令行参数的main 形式。请注意,程序名称本身作为第一个参数传递,因此您的文件名应该在 'argv[1]' 中:

    int main(int argc, char *argv[])
    {    
        FILE *inputfile;
    
        if (argc != 2) {
            fprintf(stderr, "Usage: sdev inputfile\n");
            exit(1);
        }
    
        inputfile = fopen(argv[1], "r");
    
        if (inputfile == NULL) {
            fprintf(stderr, "Failed to open %s.\n", inputfile);
            exit(1);
        }
    
        /* do stuff */
    
        fclose(inputfile);
        return 0;
    }
    

    在这里,您必须在程序中显式打开和关闭inputfile

    【讨论】:

      【解决方案3】:

      您可以使用./sdev file.dat 传递给程序的参数来完成此操作

      int main(int argc, char* argv[]) {
      
          if (argc != 2 )
          {
              printf("Your help text here\n");
          }
      
          FILE *inputfile= fopen(argv[1], "r");
      
          [...]
      

      或者如果你想用echo "file.dat" | ./sdev阅读它,你已经用标准输入阅读了它 然后您可以扩展以读取多个文件。

      char filename[1024]
      if (fgets(filename, sizeof(filename), stdin)) {
      
          FILE *inputfile= fopen(filename, "r");
      
          [...]
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-29
        • 1970-01-01
        • 1970-01-01
        • 2021-02-17
        • 1970-01-01
        • 2017-04-07
        • 2010-11-21
        • 1970-01-01
        • 2023-03-16
        相关资源
        最近更新 更多