如何从 bash 终端获取输入到我的程序中

How to get input from bash terminal into my program

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

One function (function #2) of your program is to read in the content of an instance file. To read in the file " instance10 001.txt " you will execute the command:

NameOfProgram -i instance10 001.txt

Here " -i " is the command-line option that indicates the succeeding argument is the input filename.

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

/* 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 吗?

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

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

语言:c99;编译器:gcc

这看起来像是 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 returns 成功填充的参数数量,在本例中,始终为 1。
请注意,代码的其余部分可能存在类似问题。