i have this code:

int main(int argc, char* argv[])
{
   remove(debug_file);

    count=0;//count d number of seeding
    for (int i = 1; i < argc; i++)
       {
	/* Check for a switch (leading "-") */

	if (argv[i][0] == '-')
	  {
	     /* Use the next character to decide what to do. */

	     switch (argv[i][1])
	      {

		case 'i':  strcpy (input_file,argv[++i]);
			   printf ("Input file =  %s\n", input_file);
			   break;
                case 'o':  strcpy (output_file,argv[++i]);
			   printf ("Output file =  %s\n", output_file);
			   break;

                            
               case 's':   strcpy (seed,argv[++i]);
                           strcpy (seed_array[count],seed);
                           seeding();
                           count++;
                           break;
                          


            default:   printf ("Invalid selection \n");
                       break;
	   }
	}
    }

   CleanUp();
   printf ("Running Completed......\n");
   getch();

   return 0;
}

lets say i have this parameter:
-i fruit.flt -o out -t 2 -s flower, -s oren, -s banana,

there r 3 type of seeds:flower,oren and banana which r need to be written in output file.before that, i have to process the data in seeding() function.because of the system found 3 times of -s, the function seeding() entered 3 times.what should i do if i want the system enter the function seeding() only once after the 3rd -s (banana) is processed.means,when count=3 then only go to fucntion seeding().

really blur

Dani AI

Generated

This is a common pattern: gather every -s argument while parsing, then call the processing function once when parsing is finished. and were pointing in the right direction: remove the per--s call from the switch and defer the actual work until after the argument loop. The reliable way is to store each seed (with bounds checking) and then invoke seeding(...) once with the collected list.

A compact approach using a dynamic array:

char **seeds = NULL;
int seed_count = 0;

/* inside your argv loop, when you detect -s and have the next token */
char *arg = argv[++i];         /* check i < argc first */
char *dup = strdup(arg);       /* or malloc+strcpy if strdup is unavailable */
seeds = realloc(seeds, (seed_count + 1) * sizeof *seeds);
seeds[seed_count++] = dup;

/* after the argv loop */
if (seed_count > 0) {
    seeding(seeds, seed_count);    /* pass the list so seeding runs only once */
}

/* free memory when done */
while (seed_count--) free(seeds[seed_count]);
free(seeds);

Extra tips: handle missing values (ensure i+1 < argc before consuming), trim trailing commas/spaces (the input examples show commas) or split a single -s value containing comma-separated names using strtok so a single -s flower,oren works. Use getopt/getopt_long for cleaner parsing and to handle combined switches. Check realloc/malloc results and avoid fixed-size buffers with unchecked strcpy to prevent overruns.

This keeps parsing isolated from processing, makes seeding testable (pass the array and count), and is robust for a random number of -s flags as in s scenario.

Recommended Answers

All 8 Replies

Don't put it in the switch.

Don't put it in the switch.

so, if else is better in my case?
could u please explain a bit about it?because im a new leaner.really need help

You can call the function immediately before you call cleanup either by itself or after checking for count.

You can call the function immediately before you call cleanup either by itself or after checking for count.

i have no idea at all,seriously!!!

What I meant was:

comment out seeding() on line 27.

Add the following on line 38

if(count)
{
       seeding();

}

What I meant was:

comment out seeding() on line 27.

Add the following on line 38

if(count)
{
       seeding();

}

this means i'll seeding() everytime i found "-s" rite?but this is my problem.i want to call seeding() after the last "-s".but in this example,there r only 3 "-s", in real case its a random number of "-s".
any idea about this?

No. You will be calling seeding only after you have finished processing all your input parameters from command line.

Line 38 is outside the for loop where you are processing your command line parameters.

No. You will be calling seeding only after you have finished processing all your input parameters from command line.

Line 38 is outside the for loop where you are processing your command line parameters.

U r genius man!!!! thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.