【问题标题】:fprintf doesn't write anythingfprintf 不写任何东西
【发布时间】:2016-10-25 03:50:14
【问题描述】:

我有这个代码:

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/

什么都不返回! 注意:此行正确打印在标准输出上:

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

【问题讨论】:

  • 我想你有一个.txt 文件...
  • 看看strcpy函数定义
  • 文件名几乎打印正确,除了它从文件名的开头省略了 output/ 但是它甚至没有在当前目录中创建文件

标签: c file printf file-handling filehandle


【解决方案1】:

这些行:

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多