fprintf 不写任何东西

fprintf doesn't write anything

我有这个代码:

105 void draw_detections(char * image_file_name, image im, int num, float thresh, box *boxes, float **probs, char **names, image *labels, int classes)
 106 {
 107     int i;
 108     FILE * fptr;
 109     char filename[100];
 110     strcpy(filename,"output/");
 111     strcpy(filename,image_file_name);
 112     strcpy(filename, ".txt");
 113     printf(filename);
 115     fptr = fopen (filename, "wb");
 116     printf(fptr);
 118     if (fptr == NULL) {
 119         fprintf(stderr, "Can't open input file in.list!\n");
 120         exit(1);
 122     }
 123     for(i = 0; i < num; ++i){
 124         int class = max_index(probs[i], classes);
 125         float prob = probs[i][class];
 126         if(prob > thresh){
 127             //int width = pow(prob, 1./2.)*30+1;
 128             int width = 8;
 129             printf("%s: %.0f%%\n", names[class], prob*100);
 130             fprintf(fptr, "%s,%.0f%%\n", names[class], prob*100);

完整的代码可以在这里找到:https://gist.github.com/eba1a5a6373b688b1b5d36624c897b90 fptr 不为空,但是没有创建文件。我该如何解决?

$ ls output/

returns没什么! 注意:此行正确打印在标准输出上:

 129             printf("%s: %.0f%%\n", names[class], prob*100);

这些行:

110     strcpy(filename,"output/");
111     strcpy(filename,image_file_name);
112     strcpy(filename, ".txt");

不会产生像output/some_name.txt

这样的字符串

每个 strcpy 调用都会覆盖目标字符串中已有的内容。

使用一个 strcpy然后在其他地方使用strcat附加到字符串。

OP:这解决了上面解释的问题:

 110     strcpy(filename,"output/");
 111     strcat(filename,image_file_name);
 112     strcat(filename, ".txt");
 113     printf(filename);