• C

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
SkewdLogixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
ankuratvbCommented:
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");

0
 
ankuratvbCommented:
sample command line call would be:

./program_utilization.exe myinputfile.dat myoutputfile.dat myerrorfile.dat
0
 
ankuratvbCommented:
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);
}
0
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

 
ankuratvbCommented:
>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
0
 
SkewdLogixAuthor Commented:
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
0
 
ankuratvbCommented:
int main(int argc,char *argv[])
{
}

Using this declaration for main() what happens is that when u call ur program as:

./sample.exe myinputfile.dat myoutputfile.dat myerrorfile.dat

then ,
argv[0]="sample.exe"
argv[1]="myinputfile.dat"
argv[2]="myoutputfile.dat"
argv[3]="myerrorfile.dat"

i.e. the command line arguments are automatically stored in these variables and u can use
them to open ur files using fopen().
These variables are local to main() and available anywhere inside main()
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
SkewdLogixAuthor Commented:
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
0
 
SkewdLogixAuthor Commented:
Ankuratvb,

Found the problem. Thanks for your help.

SkewdLogix
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.