Improve company productivity with a Business Account.Sign Up

x
?
Solved

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

Posted on 2004-03-26
8
Medium Priority
?
247 Views
Last Modified: 2010-05-19
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
0
Comment
Question by:SkewdLogix
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10688905
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10688923
sample command line call would be:

./program_utilization.exe myinputfile.dat myoutputfile.dat myerrorfile.dat
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10689006
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10689030
>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
 

Author Comment

by:SkewdLogix
ID: 10689232
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
 
LVL 9

Accepted Solution

by:
ankuratvb earned 2000 total points
ID: 10689516
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
 

Author Comment

by:SkewdLogix
ID: 10690074
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
 

Author Comment

by:SkewdLogix
ID: 10692474
Ankuratvb,

Found the problem. Thanks for your help.

SkewdLogix
0

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

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.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

606 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question