Solved

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

Posted on 2004-03-26
8
238 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
 
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 500 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

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixing C++ and C code elegantly 10 153
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 59
Need example 5 107
Problem to save 10 147
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now