Mona Jalal Mona Jalal - 1 month ago 12
C Question

fprintf doesn't write anything

I have this code:

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


The complete code can be found here: https://gist.github.com/eba1a5a6373b688b1b5d36624c897b90
fptr is not null however no file gets created. How can I fix it?

$ ls output/


returns nothing!
Note: this line gets printed on the stdout correctly:

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

Answer

These lines:

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

does not produce a string like output/some_name.txt

Each strcpy call overwrites what is already in the destination string.

Use one strcpy and then use strcat the other places to append to the string.

OP: This fixed the problem as explained above:

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