Do not use on any
shared computer
August 29, 2008 09:08pm pdt
 
[x]
Attachment Details

Passing Argument

I need some help with passing an argument from >> input << to >> output <<.

When this program is executed (from command window), the user must enter "name of executable", "image input filename" [1st argument], "image output filename" [2nd argument].

The programs then does do some "processing" (what it does is not pertaining to this question).

The the program sent feedback to screen ("You have successfully unhidden the image")  

Now, here's what I need some help with.  Instead of just indicating "image", I'd like to include the output filename that the user entered [2nd argument].    How do I pass that value from the input section to the fprintf statement at the bottom of the program?

Thanks,
EEH



************
// The main function has always access to the programs.
// argc = counter; argv = array of strings.
int main(int argc, char **argv){


      // Initialize variables:
        // ... done here


      // The read program is invoked by: readimage inputfile outputfile.
      // Program requires at least 3 arguments:
      // 1. "call program"
      // 2. "input pgm"
      // 3. "output pgm"; the modified image will be written to this outfile file

      if (argc < 3) {
            fprintf(stderr,"Usage: readimage input-file-name output-file-name\n");
            exit(0);
      }
      fpIn = fopen(argv[1],"rb");                        // argv[1] contains the filename
      if (fpIn == NULL) {
            fprintf(stderr,"%s either cannot be read or does not exist\n", argv[1]);
            exit(-1);
      }
      string = (char *) calloc(256,1);            // initialized dynamic memory allocation
      while (!doneReading && (c = (char) fgetc(fpIn)) != '\0')
      {
            switch(c) {
case 'P':
      c = (char) getc(fpIn);
      switch(c) {
case '2':
      numberOfBands = 1;
      // ppmType = PPMASCII;
      break;
case '3':
      numberOfBands = 3;
      // ppmType = PPMASCII;
      break;
case '5':
      numberOfBands = 1;
      // ppmType = PPMGRAY;
      break;
case '6':
      numberOfBands = 3;
      // ppmType = PPMRAW;
      break;
      }  

      c = (char) getc(fpIn);
      if (c != 0x0A) {
            ungetc(c,fpIn);
      }
      else {
            ungetc(c,fpIn);
            fgets(string,256,fpIn);
      }
      break;

case '#':
      fgets(string,256,fpIn);
      break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
      ungetc(c,fpIn);
      fscanf(fpIn,"%d %d %d", &numberOfColumns, &numberOfRows, &highVal);
      doneReading = TRUE;
      fgetc(fpIn);
      break;

            }    // End Switch
      }      // End While


      // Reads from the specified pointer; 1 byte, # of pixels, and file pointer
      image = (unsigned char *) malloc(totalPixels);
      l = fread(image,1,totalPixels,fpIn);
      fprintf(stderr,"fread returned %d bytes\n", l);
      imaged = (unsigned char *) malloc(totalPixels);


      // Some processing is performed here!


      // ******** Image Output Section ********
      //Generate the midterm2.pgm (modified) image
      fpOut = fopen(argv[2],"wb");

      // Write image header
      fprintf(fpOut,"P%d\n%d %d\n255\n",numberOfBands>1?6:5,numberOfColumns,numberOfRows);  
    fwrite(imaged,1,totalPixels,fpOut);          

// Feedback
fprintf(stdout,"\n\n\nYou have successfully unhidden the image!\n\n\n");                                            


      // Exit routine
      exit(0);  
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: ExpExchHelp
Solution Provided By: NickGeorghiou
Participating Experts: 3
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Accepted Solution by NickGeorghiou
Accepted Solution by NickGeorghiou:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by ExpExchHelp
Author Comment by ExpExchHelp:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by ExpExchHelp
Author Comment by ExpExchHelp:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by NickGeorghiou
Expert Comment by NickGeorghiou:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by NickGeorghiou
Expert Comment by NickGeorghiou:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by ikework

Rank: Guru

Expert Comment by ikework:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by ExpExchHelp
Thank you, Nick.   That works great!

EEH
 
 
Comment by Infinity08
If you chose to use this :

>> char string[80];
>> strcpy(string, "\n\n\nYou have successfully unhidden the image (filename: ");
>> strcat(string, arg2);          
>> strcat(string, ")!\n\n\n");
>> fprintf(stdout, string);

then note that this is far from the best way to do this in C. ikework's last post shows a way nicer and cleaner way to do this.
 
 
Comment by NickGeorghiou
EEH, thanks for recognizing that I answered your original question in a suitable manner.

For others interested, EEH reposted the follow up question here:

http://www.experts-exchange.com/Programming/Languages/C/Q_22934065.html

Ikeworks solution was correctly chosen as the Accepted Answer in this instance.
 
 
20080723-EE-VQP-34 / EE_QW_2_20070628