Link to home
Start Free TrialLog in
Avatar of SkewdLogix
SkewdLogix

asked on

Designating input and output files in the command line of an executable file

I have written a program which inputs data from an input file and outputs the resultant solution to an output file. Currently I have the file names hard coded in the body of the program. I need to know how I can enable the program to read from and write to data files of different names which will be supplied by users in the input line of the program executable call. Ideally, I would like to have generic names coded in the program so that they can be associated with the input and output files so many different files can be used.

An example would be:

./program_utilization.exe inputfile outputfile errorfile

Now the user would call the program by issuing the following command:

./program_utilization.exe myinputfile myoutputfile myerrorfile

The program should be able to associate all of the file commands to inputfile/outputfile/errorfile written in the program with the user specified myinputfile/myoutputfile/myerrorfile so that each user would have distinct input and output file names and this process then would enable multiple users at the same time.

I'm not sure how to code this in C. I need to scan the names after the executable call and have them designated as the different files depending on their position and then associated with wildcards written into the program.

Any help would be appreciated.

SkewdLogix
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

assuming main(int argc,char *argv[]) declaration:

U can simply do the following:

For input file,At the place where u open ur input file:

fopen(argv[1],"r");
since argv[0] is program name and argv[1] is the first argument.

For outputfile,At the place where u open ur output file:

fopen(argv[2],"w");


For errortfile,At the place where u open ur error file:

fopen(argv[3],"w");

sample command line call would be:

./program_utilization.exe myinputfile.dat myoutputfile.dat myerrorfile.dat
A sample program:

int main(int argc,char *argv[])
{
 FILE *ip,*op,*err;
 ip=fopen(argv[1],"r");//to read from input file
 //read from this file using fgetc,fread,fgets etc.
 op=fopen(argv[2],"w");//to write to output file
 //write to o/p file using fputc,fputs,fwrite etc.
 fclose(op);
 fclose(ip);
 err=fopen(argv[3],"w");//to write to error file
 //write to error file
 fclose(err);
}
>and then associated with wildcards written into the program.

What do u mean by this statement?Can u please elaborate about the wildcards written into
the program
Avatar of SkewdLogix
SkewdLogix

ASKER

Hi ankuratvb,

All I mean is that these files are opened and closed a few times each because of different function calls and I wanted to be able to call up the correct files at these different times by using the name supplied by the user. From your input, I believe that by naming them argv[1], argv[2] and so on, that I am in essence doing that.

I'm just in the process of trying out your solution with my program so I'll get back to you as soon as I finish.

SkewdLogix
ASKER CERTIFIED SOLUTION
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi ankuratvb,

I'm experiencing some difficulties opening the input data file for reading. When no data input file is specified, the error file and the output file are opened but when I specify an input file with data, I get a segmentation fault.

Here's a portion of the code that I am using:

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


  long int i, n, nclin, prob_type = 0;
 
  FILE *fpin;
  FILE *fpout;
  FILE *fperr;

  double *x = 0;
  double *cvec = 0;
  double *bl = 0;
  double *bu = 0;
  double *up = 0;
  double *low = 0;
  double *a = 0;
  double *h = 0;
  double *s = 0;
  double *h_orig = 0;
  double *eff = 0;
 
/* First ensure that all input and ouput files can be accessed for read and write operations */


      if((fperr = fopen(argv[3], "w")) == NULL)
        {
        Vfprintf(fperr, "\nFailure to open error file.\n");
        exit(EXIT_FAILURE);
        }
      else
          Vfprintf(fperr, "\nPortfolio Optimization.\n");
        Vfprintf(fperr, "\nError file open for writing.\n");
       
      if((fpout = fopen(argv[2], "w")) == NULL)
        {
          Vfprintf(fperr, "\nFailure to open output file.\n");
        exit (EXIT_FAILURE);
        }
      else {
        Vfprintf(fperr, "\nOutputfile open for writing.\n");
         }

      if((fpin = fopen(argv[1], "r")) == NULL)
        {
        Vfprintf(fperr, "\nFailure to open input file.\n");
        exit(EXIT_FAILURE);
        }
      else
        Vfprintf(fperr, "\nInput file open for reading.\n");
       
/* Read the type of optimization problem to be solved. */

Vfscanf(fpin, "%ld", &prob_type);

  /* Read the actual problem dimension.
   * n = the number of equities. */

Vfscanf(fpin, "%ld", &n);

/* Read the actual the number of general linear constraints.
 * nclin = number of constraints */

Vfscanf(fpin, "%ld", &nclin);

-----------------------------------------------------------------------------------------------

The segmentation fault only occurs when I try to open the input file. It appears to be something to do with this operation.

When I had the files hard coded in the program, the main function call was specified as

int main()
{

Would this have anything to do with my problem?



SkewdLogix
Ankuratvb,

Found the problem. Thanks for your help.

SkewdLogix