hi and thanks for advance,
i have a problem to compare between 2 arrays.
i have a file name fruit_file which has a content like this:
apple
orange
mango
banana
papaya

and i have an seed array from arg list which is;
mango
lime
pineapple
peach

i want to copy seeds from seed_array to fruit_file but not for same fruit.so i have to compare array and file so that no duplicate fruit inside the file.
below is my code and i cant have good output.and this is my output (in fruit_file)
apple
orange
mango
banana
papaya
lime
lime
pineapple
lime
pineapple
peach

i dont know why there r duplicate fruit inside the fruit_file.is it because of my seed_array or what?

char seed[100]="";
char seed_array[100][100];
int main(int argc, char* argv[])
{
       count=0;//count d number of seeds
    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 's': strcpy (seed,argv[++i]);
                          seed_found=true;
                          strcpy (seed_array[count],seed);
                          count++;
                              seeding();

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


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

   return 0;
}

void seeding()
{
FILE *fruit_file;
   if ((fruit_file= fopen(fruit, "a+"))
       == NULL)
  {
    fprintf(stderr, "Cannot open output file.\n");
    exit(0);
  }
  char fruit_array[100][100];
  int row=0;
  char *p=NULL;
  while (fgets(fruit_array[row],100,fruit_file) !=NULL)
  {
    if(p=strchr(fruit_array[row],'\n'))
      {
        *p='\0';
      }
    row++;
  }
            for(int i=1;i<=count;i++)
               {
                
                   printf("seed=%s\n",seed_array[i]);
                  for(int bil=0;bil<=row;bil++)
                   {
                     if(strcmp(fruit_array[bil],seed_array[i]) != 0)
                      {
                           fprintf (fruit_file,"%s\n",seed_array[i]);
                      }
                   }

                }
}

really need help from u guys!!!!

Can you give the actual command used to invoke the program?

for(int i=1;i<=count;i++)

Here , the first element in the seed array is getting ignored. This could be the reason that you are getting multiple entries of the same fruit in your result file.

Edited 6 Years Ago by thomas_naveen: reasoning error

Can you give the actual command used to invoke the program?

for(int i=1;i<=count;i++)

Here , the first element in the seed array is getting ignored. This could be the reason that you are getting multiple entries of the same fruit in your result file.

do u mean i need to start with i=0?
actually i combine the code with other function and its a long code so i juz copied for fruit's code only.

#pragma hdrstop
#include <stdio.h>
#include <time.h>
#include <dos.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <sys\stat.h>
#include <fcntl.h>
#include <io.h>
#include <time.h>

char fruit[MAX_FILENAME]="";
char seed[100]="";
char seed_array[100][100];
int main(int argc, char* argv[])
{
       count=0;//count d number of seeds
    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 's':     strcpy (seed,argv[++i]);
                                              seed_found=true;
                                              strcpy (seed_array[count],seed);
                                              count++;
                                              seeding();  
                                              break;      
                           default:      printf ("Invalid selection \n");
                                             break;
         }
    }
       }


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

   return 0;
}

void seeding()
{
   FILE *fruit_file;
   if ((fruit_file= fopen(fruit, "a+"))  == NULL)
      {
            fprintf(stderr, "Cannot open output file.\n");
             exit(0);
       }
  char fruit_array[100][100];
  int row=0;
  char *p=NULL;
  while (fgets(fruit_array[row],100,fruit_file) !=NULL)
    {
                  if(p=strchr(fruit_array[row],'\n'))
                       {
                                *p='\0';
                         }
                  row++;
     }
    for(int i=1;i<=count;i++)
      {   
                   printf("seed=%s\n",seed_array[i]);
                  for(int bil=0;bil<=row;bil++)
                      {
                                 if(strcmp(fruit_array[bil],seed_array[i]) != 0)
                                          {
                                               fprintf (fruit_file,"%s\n",seed_array[i]);
                                           }
                     }

       }
}

really need help from u guys!!!!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.