如何从bash终端输入我的程序

时间:2016-10-09 04:03:13

标签: c bash c99

我的一项任务有以下问题:

  

程序的一个功能(功能#2)是读入实例文件的内容。读入   文件"   instance10   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

1 个答案:

答案 0 :(得分:1)

这看起来像你的if语句中的一个简单错误。你只是说,如果inputfile是1(这意味着-o必须是argv [0]),它将打开inputfile。

vm.maquina_id

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

visitamaquina

此外,在这段代码中:

reporteproducto

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