Hi all,
My problem is regarding to my last post about argument. that is solved but it create a new problem. The last data in debug_file is not written in output file. I think its because of looping in the argument.How to solve this?

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;

}

this is my input
-i fruit.flt -o out -t 2 -s flower, -s oren, -s banana,

first, i put all these seeds into the debug_file,then from the debug file only pass to output file because of operating sumthing.But,this cause the problem which is the last data:banana is not written in the output file.How this happen and how to solve this?

Recommended Answers

All 4 Replies

I don't see any writing to any file. You might want to open/write/close a file and you might get banana into the file.

Hi all,
My problem is regarding to my last post about argument. that is solved but it create a new problem. The last data in debug_file is not written in output file. I think its because of looping in the argument.How to solve this?

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;

}

this is my input
-i fruit.flt -o out -t 2 -s flower, -s oren, -s banana,

first, i put all these seeds into the debug_file,then from the debug file only pass to output file because of operating sumthing.But,this cause the problem which is the last data:banana is not written in the output file.How this happen and how to solve this?

sorry for my unfinished code.this is the rest:

void seeding()
{
     FILE *out_debug;
     PrepareOutputHeader(); //this is to prepare the header of the report...
     char debug_array[100][100];

 // open debug and write the randomize test cases
//for record; there r data casses already in debug_file
  if ((out_debug = fopen(debug_file, "a+")) == NULL)
      {
             fprintf(stderr, "Cannot open output file.\n");
             exit(0);
      }

  int row=0;
  char *p=NULL;
   while (fgets(debug_array[row],100,out_debug) !=NULL)
        {

                      if(p=strchr(debug_array[row],'\n'))
                            {
                                  *p='\0';
                             }

                     row++;

        }

    ////printf seeds to debug file.compare with the exist data in debug so that
   ////no conflict or duplicate seed/fruit.

  int check;

       for(int j=0;j<=row;j++)
             {
                  if(strcmp(debug_array[j],seed_array[count])== 0)
                  break;

                  else check++;

                  if (check ==row)
                    {
                        fprintf (out_debug,"\n%s",seed_array[count]);
                    }

             }



 ReformatOutputFile();

  fflush (out_debug);
  fclose (out_debug);
}



//--------------------------------------
// Reformat debug_file to output file in
// combinator format
//--------------------------------------

void  ReformatOutputFile()
 {

   FILE *out,*out_debug;
   int no_of_generated_test_cases=0;

    printf ("Reformatting output ....\n");
   if ((out_debug = fopen(debug_file, "rt"))
       == NULL)
    {
      fprintf(stderr, "Cannot open input file.\n");
      exit(0);
    }

  if ((out = fopen(output_file, "at"))
                     == NULL)
                  {
                       fprintf(stderr, "Cannot open output file.\n");
                       exit(0);
                   }


   fseek(out_debug,0, SEEK_END);
   long length = ftell (out_debug);


   fseek(out_debug,0,SEEK_SET);
   long curntpos = ftell (out_debug);

   // read the debug file                                  ,'
   while (curntpos<length)
    {
       fgets (big_string,800,out_debug);

       if (!strstr(big_string,"REPETITIVE"))
        {
         fprintf (out,"\n%s\n",separator);
         fprintf (out, "\tTest case %d\n",no_of_generated_test_cases++);

         fprintf (out,"%s\n",separator);

         int token_count = CountToken(big_string,',');
         char *token_value;
         // tokenize big string
         for (int i=0;i<token_count;i++)
          {
           if (i==0)
            token_value= strtok (big_string,",");
           else
            token_value = strtok (NULL,",");

           fprintf (out,"arglist : arglist[%d]=%s\n",i,token_value);
           printf ("arglist : arglist[%d]=%s\n",i,token_value);//debugging
          }
        }
       curntpos = ftell (out_debug);

    }


   fclose (out_debug);
   fclose (out);

 }

Tell you what. Format your code so we can follow it using this information paying close attention to the indentation information, then give us an idea what you are trying to do with the code and an idea how it's supposed to work. And explain in detail what's wrong and where you think it's wrong. Then we might be able to come up with some ideas. As it is, I don't really have a clue what's going on here because the code is a mystery, and it's really hard to read.

I have solved this problem.

This problem caused by i didnt flush the debug_file before i call ReformatOutputFile(). So, the last data will always in buffer.
In my case,
-s flower, -s oren, -s banana, as an input.
i call seeding() everytime i got an 's' . In seeding,i put all the inputs int debug_file then later i have to send the data to the report which is output_file (coded in ReformatOutputfile()). But, i call ReformatOutputFile before i flush debug_file,so that makes the data last still in buffer.

so, the solution that i make is flush the debug_file first then only call ReformateOutputFile() so that all data in debug_file will be written in output_file.

This is my ignorance that cause me a problem......

sorry bothering u guys and thanks for ur concern WaltP!!!

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.