mardi 30 juin 2015

How to fix sprintf error


The below code works just fine if I comment the sprintf line at the very bottom

int doStepOneAndTwo(){
FILE *fp, *source, *target, *fp1; 
   PROT prot;
   int i, j;
   int k, counter;
   int n_conf;
   int number_of_dir;
   int number_of_line;
   char sbuff[256];
   char str[256];
   char str1[256];
   int ch;
   const char *a[5];

   number_of_line = 140;
   number_of_dir = ceil( number_of_line /30) + 1;
   a[0] = "submit.sh";
   a[1] = FN_RUNPRM;   // run.prm
   a[2] = env.rename_rules; // name.txt
   a[3] = env.inpdb; // prot.pdb
   a[4] = STEP2_OUT; // step2_out.pdb

   for (i=1; i<=number_of_dir; i++)
    {

      sprintf(str,"_%d", i);
      mkdir(str,"0755"); // Create temporary directories

      for (j=0;j<5;j++)
      {
          sprintf(str,"%s", a[j]);
          source = fopen(str, "r");
          if( source == NULL ) 
            {
                printf("Error in doStepOneAndTwo, can't open file source \n");
                return USERERR;
            }
          sprintf(str,"_%d/%s", i, a[j]);
          target = fopen(str, "w");
          if( target == NULL ) 
            {
                fclose(source);
                printf("Error in doStepOneAndTwo, can't open file target %s \n",str);
                return USERERR;
            }


          while( (ch = fgetc(source)) != EOF) {
              fputc(ch, target);
          }

          //if(k!=1){printf("fuck me");}else{printf("fuck you \n");}
          //k++;

      }

    }
    fclose(source);
    fclose(target);

for (k=1; k<2; k++)
    {
        printf("Yes %d \n",k);

        sprintf(str1,"_%d/run.prm",k); // If I comment this line everything works just fine 
        //chdir("_1");
        fp1=fopen(FN_RUNPRM, "r");
    }

return 0;

}

The error I get is "Error in doStepOneAndTwo, can't open file target _1/submit.sh" . Basically, the target file is NULL. I'm new to c and don't know what I'm doing wrong.


Aucun commentaire:

Enregistrer un commentaire