0

One of my assignments has the following question:

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.

This is what I have done so far, mostly a skeleton:

/* 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;
}

I am wondering how I can read the name of the input file from bash terminal. Should I use scanf?

how do I get what the user has inputted as the input file? Like for example if the user is running my program from bash with ./myprogram -i instance10_001.txt, how can I open the inputted file in my program?

PS I am using my Ubuntu terminal to access my lab computer via ssh.

Language: c99 ; Compiler: gcc

1 Answer 1

1

This looks like a simple error in your if statment. You're saying if, and only if, inputfile is 1 (which means -o must have been argv[0]) it will open 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");
    }

Also, there's another problem here, in which you assign fp to a function and then reopen the file already opened in 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;
    }

Also, in this block of code:

      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 the number of arguments successfully filled, which in this case, will always be 1.
Note that similar problems may be present in the remainder of the code.

Sign up to request clarification or add additional context in comments.

6 Comments

The first if statement checks if the user entered the correct command line that is either myprogram -i inputfile -o outputfile or vice versa. If the user inputs for say myprogram -z inputfile -o outputfile it will print the error and exit.
@ImtiazRaqib: I know, however when you type if (inputfile == 1), the fopen will only happen if the inputfile == 1, in which case it must be the first argument on the command line: In ./a.out -i file, ./a.out is argument 0, -i is argument 1, and file is argument 2, and therefore inputfile will be 2.
Taken I change my if statement, how do I get what the user has inputted as the input file? Like for example if the user is running my program from bash with ./myprogram -i instance10_001.txt, how can I open the inputted file in my program?
argv[inputfile], you did it right. fp=fopen(<filename>,"r"); opens <filename> with read access and stores the file pointer into fp. Once you have opened it once, until you run fclose(fp);, the file pointer will always be stored in fp.
argv is an array of string pointers which for a single argument. Your inputfile variable stores the argument number of the input file specified. So, argv[inputfile] will give the user inputted filename.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.