Nick,
how do I string the following togehter?
You have successfully unhidden the image (filename: imagename)!
Main Topics
Browse All TopicsI 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,
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
fwrite(imaged,1,totalPixel
// Feedback
fprintf(stdout,"\n\n\nYou have successfully unhidden the image!\n\n\n");
// Exit routine
exit(0);
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi again,
You can actually do this a number of ways.
If you wish to continue to use fprintf then one way you can do it is to use a stringstream to create your string. For this you will need to #include <sstream> at the top of your file and then do the following:
std::stringstream msg;
msg << "You have successfully unhidden the image (filename: " << arg2 << ")!";
fprintf(stdout, msg.str().c_str());
But a simpler way is to not use fprintf for this. Instead if you put #include <iostream> at the top of your file you can simply replace your fprintf line with the following:
std::cout << "You have successfully unhidden the image (filename: " << arg2 << ")!" << std::endl;
Hope this helps,
Cheers,
Nick
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.
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-exchang
Ikeworks solution was correctly chosen as the Accepted Answer in this instance.
Business Accounts
Answer for Membership
by: NickGeorghiouPosted on 2007-11-01 at 19:13:41ID: 20197950
Hi,
Try the following:
char* arg2 = argv[2];
fprintf(stdout, arg2);
Cheers,
Nick