【问题标题】:How to get input from bash terminal into my program如何从 bash 终端获取输入到我的程序中
【发布时间】:2016-10-09 13:57:44
【问题描述】:

我的一个作业有以下问题:

程序的一个功能(功能#2)是读入实例文件的内容。读入 文件 ” 实例10 001.txt " 你将执行命令:

NameOfProgram -i instance10 001.txt

这里“ -i " 是命令行选项,指示后面的参数是 输入文件名。

这是我到目前为止所做的,主要是骨架:

/* Assignment 1 */

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

int main(int argc, char *argv[]) 

{

  FILE *fp;

  int max_x, max_y, num_pt, rand_inst;
  int *x_coordinate, *y_coordinate;

  int inputfile = 0, outputfile = 0;
  int i;

  if (argc == 1)
    {
      /* to generate random instances, accepting parameters from stdin */
      printf("Generating random instances...");
      printf("Enter the circuit board size MAX_X MAX_Y:  ");
      scanf("%d %d", &max_x, &max_y);
      printf("Enter the number of points NUM_PT:  ");
      scanf("%d", &num_pt);
      printf("Enter the number of random instances to be generated:  ");
      scanf("%d", &rand_inst);
      return 1;
    }
  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "-i") == 0)
          inputfile = i+1;
      else if (strcmp (argv[i], "-o") == 0)
          outputfile = i+1;
    }
  if (inputfile == 0)
    {
      /* invalid comman line options */
      printf("\nIncorrect command-line\n");
      printf("myprogram [-i inputfile [-o outputfile]]");
      return -1;
    }

  **/* THIS IS WHERE I NEED HELP */**
  if (inputfile == 1)
    {
      fp = fopen(/*Name of the input file (instance10_001.txt) */, "r")
    }


  if ((fp = fopen(argv[inputfile], "r")) == NULL)
    {
      /* open file error */
      return -2;
    }
  while (fscanf(fp, "%d", &max_x) != 1)
    {
      if (ferror(fp))
        {
          /* read error */
          fclose(fp);
          return -3;
        }
      if (feof(fp))
        {
          /* no integer to read */
          fclose(fp);
          return -4;
        }
      fscanf(, "%*[^\n]"); /*skip the rest of line */
    }
  if (fscanf(fp, "%d", &max_y) != 1)
    {
      /* max_y not following max_x */
      fclose(fp);
      return -5;
    }
  while (fscanf(fp, "%d", &num_pt) != 1)
    {
      if(ferror(fp))
       {
          /* read error */
          fclose(fp);
          return -6;
        }
      if (feof(fp))
        {
          /* no integer to read */
          fclose(fp);
          return -7;
        }
       fscanf(fp, "%*[^\n]"); /* skip the rest of line */
    }

  x_coordinate = (int *)malloc(num_pt * sizeof(int));
  y_coordinate = (int *)malloc(num_pt * sizeof(int));
  for (i = 0; i < num_pt; i++) 
    {
      while (fscanf(fp, "%d", &x_coordinate[i]) != 1) 
        {
          if (ferror(fp)) 
            {
              /* read error */
              fclose(fp);
              return -8;
            }


if (feof(fp)) 
        {
          /* no integer to read */
          fclose(fp);
          return -9;
        }
      fscanf(fp, "%*[^\n]"); /* skip the rest of line */
    }
      if (fscanf(fp, "%d", &y_coordinate[i]) != 1) 
    {
      /* y_coordinate not following x_coordinate */
      fclose(fp);
      return -10;
    }
    }
  fclose(fp);
  if (outputfile > 0) 
    {
    if ((fp = fopen(argv[outputfile], "w")) == NULL) 
      {
    /* open file error */
    return -2;
      }
    fprintf(fp, "##################################################\n");
    fprintf(fp, "#%s\n", argv[inputfile]);
    fprintf(fp, "#area [0, MAX_X] x [0, MAX_Y]\n");
    fprintf(fp, "%d\t%d\n", max_x, max_y);
    fprintf(fp, "#number of points NUM_PT\n");
    fprintf(fp, "%d\n", num_pt);
    fprintf(fp, "#coordinates\n");
    for (i = 0; i < num_pt; i++) 
      {
    fprintf(fp, "%d\t%d\n", x_coordinate[i], y_coordinate[i]);
      }
    fprintf(fp, "#end of instance\n");
    fclose(fp);
    }
  else 
    {
      printf("##################################################\n");
      printf("#%s\n", argv[inputfile]);
      printf("#area [0, MAX_X] x [0, MAX_Y]\n");
      printf("%d\t%d\n", max_x, max_y);
      printf("#number of points NUM_PT\n");
      printf("%d\n", num_pt);
      printf("#coordinates\n");
      for (i = 0; i < num_pt; i++) 
    {
      printf("%d\t%d\n", x_coordinate[i], y_coordinate[i]);
    }
      printf("#end of instance\n");
    }
  free(x_coordinate);
  free(y_coordinate);

  return 0;
}

我想知道如何从 bash 终端读取输入文件的名称。我应该使用 scanf 吗?

如何获取用户输入的内容作为输入文件?例如,如果用户使用 ./myprogram -i instance10_001.txt 从 bash 运行我的程序,我如何在我的程序中打开输入的文件?

PS 我正在使用我的 Ubuntu 终端通过 ssh 访问我的实验室计算机。

语言:c99;编译器:gcc

【问题讨论】:

    标签: c bash c99


    【解决方案1】:

    这看起来像是您的 if 语句中的一个简单错误。您是说当且仅当 inputfile 为 1(这意味着 -o 必须是 argv[0])时,它才会打开 inputfile。

      if (inputfile == 0)
        {
          /* invalid command line options */
          printf("\nIncorrect command-line\n");
          printf("myprogram [-i inputfile [-o outputfile]]");
          return -1;
        }
      else /* if inputfile is not equal to 0, then this will execute. */
        {
          fp = fopen(argv[inputfile], "r");
        }
    

    另外,这里还有一个问题,你将 fp 分配给一个函数,然后重新打开已经在 fp 中打开的文件:

    /* removed fp = fopen (a function) */
      if (fp == NULL) /* You already opened the file; no need to open again until fclose */
        {
          /* open file error */
          return -2;
        }
    

    另外,在这段代码中:

          while (fscanf(fp, "%d", &x_coordinate[i]) != 1) 
            {
              if (ferror(fp)) 
                {
                  /* read error */
                  fclose(fp);
                  return -8;
                }
    
    
    if (feof(fp)) 
            {
              /* no integer to read */
              fclose(fp);
              return -9;
            }
          fscanf(fp, "%*[^\n]"); /* skip the rest of line */
        }
    

    Fscanf 返回成功填充的参数数量,在这种情况下,始终为 1。
    请注意,代码的其余部分可能存在类似的问题。

    【讨论】:

    • 第一个if 语句检查用户是否输入了正确的命令行myprogram -i inputfile -o outputfile,反之亦然。如果用户输入说myprogram -z inputfile -o outputfile,它将打印错误并退出。
    • @ImtiazRaqib: 我知道,但是当你输入if (inputfile == 1) 时,fopen 只会在 inputfile == 1 时发生,在这种情况下它必须是命令行上的第一个参数:在 @ 987654328@,./a.out 是参数 0,-i 是参数 1,file 是参数 2,因此 inputfile 将为 2。
    • 采取我改变我的if语句,我如何得到用户输入的输入文件?例如,如果用户使用 ./myprogram -i instance10_001.txt 从 bash 运行我的程序,我如何在我的程序中打开输入的文件?
    • argv[inputfile],你做对了。 fp=fopen(&lt;filename&gt;,"r"); 以读取权限打开 并将文件指针存储到 fp 中。一旦你打开它一次,直到你运行fclose(fp);,文件指针将始终存储在fp中。
    • argv 是一个字符串指针数组,用于单个参数。您的 inputfile 变量存储指定的输入文件的参数号。所以,argv[inputfile] 会给出用户输入的文件名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 2014-09-14
    相关资源
    最近更新 更多