Hello guys.
I guess I need some help with a program I try to create.
In the end I want to have a seam carving program. And yes it is kind of an homework.
But first things first. The problem I actually have is that I try to understand and make good use of it. But I fail at a single point and don't know why.
In my opinion, the following, incomplete, code should handle all the incoming from the console/terminal and react in some way.
For example if I type
Code:
/build/carve -s /data/owl.ppm
it should not print the error message for a not found/valid picture (data/owl.ppm is a valid picture). But it does. If someone can help me I would be very happy!
The code I have now is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>
void help ()
{
fprintf (stderr, "\nCarve, a procedure for seam carving.\n\nUsage: carve [OPTIONS] <File>\n\n\nOptions:\n[-h]: Opens the help menu.\n\n[-s] <File>: Shows different statistics about the picture.\n\n[-p] <File>: Shows 'Spaltenindizes' of the picture.\n\n[-n <number>] <File>: Applies the algorithm n times on the picture.\nIf no <number> is given the algorithm runs until the end.\n\n\nThe file:\n<File>: The file has to be a '*.ppm' file.\nPlease use this format.\n\n");
}
void stats ()
{
fprintf(stderr, "space3\n");
abort ();
}
int main (int argc, char **argv)
{
int helpflag = 0;
int statflag = 0;
int spaltenflag =0;
int algovalue = 0;
int c;
opterr = 1;
while ((c = getopt (argc, argv, "hspn:")) != -1)
switch (c)
{
case 'h': helpflag = 1; break;
case 's': statflag = 1; break;
case 'p': spaltenflag = 1; break;
case 'n': algovalue = optarg; break;
case '?':
if (optopt == 'n')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option '-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);
return 1;
default:
abort ();
}
int File = ((argc-1) == optind);
if (File == 1)
{
FILE *Image = fopen (argv[optind],"r");
if ((algovalue || statflag || spaltenflag) && !Image)
{
fprintf (stderr,"Something went wrong. No picture found.\n");
exit(EXIT_FAILURE);
}
}
if (helpflag == 1)
help();
else if (statflag == 1)
stats();
else if (spaltenflag == 1)
fprintf(stderr,"space\n");
else if (algovalue != 0)
fprintf(stderr,"space2\n");
return 0;
}
Thanks in advance! :)