Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何修复sprintf错误_C_Linux - Fatal编程技术网

C 如何修复sprintf错误

C 如何修复sprintf错误,c,linux,C,Linux,如果我在最底部注释sprintf行,下面的代码就可以正常工作 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]

如果我在最底部注释sprintf行,下面的代码就可以正常工作

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("foo");}else{printf("bar \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;

}
int-dosteponeandtowo(){
文件*fp、*source、*target、*fp1;
PROT-PROT;
int i,j;
INTK,计数器;
int n_conf;
_dir的整数_;
_线的整数_;
char-sbuff[256];
char-str[256];
char-str1[256];
int-ch;
常量字符*a[5];
_线的_数=140;
_-dir的数量=ceil(_-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

对于(i=1;i我发现很难相信注释掉
sprintf()
会让程序为您工作。这样做不会让它为我工作

主要问题似乎出现在
mkdir()
调用中。传递给该系统调用的文件模式应该是整数(特别是
模式
)模式,但您正在传递一个指针,该指针已转换为整数。这完全不是您想要的。在我的特定情况下,目录的结果模式不提供所有者写入权限,导致与您描述的错误消息完全相同

这个可选的
mkdir()
调用确实使您的程序为我工作,无论最后的
sprintf()
是否被引用:

mkdir(str, 0755);
请注意,不带引号的
0755
是一个八进制(以8为基数)的文字,因为它以数字
0
开头,而不是十六进制文字。这样写比较传统,不过:

mkdir(str, S_IRWXU | S_IRGRP | SIXGRP | S_IROTH | S_IXOTH);

您可能需要包含一个或多个
sys/types.h
sys/stat.h
fcntl.h
来获取模式宏的定义。

是否在与
submit.sh
相同的目录下运行此文件?如果不是,则需要指定
submit.sh
文件的完整路径。是否希望此文件退出st,或者你打算创建它吗?fopen会创建一个文件,如果它是dne,但不是目录。submit.sh存在于一个目录中,我正在将它复制到另一个目录。如果我在最后一个for循环中注释sprintf行,一切都会运行。我不明白的是,为什么第一个循环会受到最后一个循环的影响。是的,修复了它,取消引用0755 in mkdir()。