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,numbe
rOfColumns
,numberOfR
ows);
fwrite(imaged,1,totalPixel
s,fpOut);
// Feedback
fprintf(stdout,"\n\n\nYou have successfully unhidden the image!\n\n\n");
// Exit routine
exit(0);