Greetings.
I'm writing a program that opens files given by /route/file_name. Such program receives the file_name via stdout from bash, i.e ./main < <(ls ../script/) where ls ../script/ gives the output dummy0.log, dummy1.log ... dummyn.log
Code:
#include <stdio.h>
#include <string.h>
#include "search.h"
#include "dbg.h"
int search_line(void){
char file_name[BUFSIZE];
char route_name[BUFSIZE];
int c;
FILE *log;
/* Reads file_name from stdout */
while (fgets(file_name, BUFSIZE, stdin) != NULL){
/* Only read file if such has *.log filetype */
if ( is_approved_type(file_name) ){
/* Variable subtitution pattern */
sprintf(route_name, "../script/%s", file_name);
/* Does not work,
* in gdb, command 'printf "%s", route_name" gives
* "../script/dummy0.log" */
log = fopen(route_name, "r");
/* The following does work and opens file
* log = fopen(../script/dummy0.log */
/* Checks stream related errors*/
check(log, "Error opening %s", route_name);
/* Checks all file's content */
while(1){
c = fgetc(log);
if ( feof(log) )
break;
printf("%c", c);
}
/* Close stream */
fclose(log);
/* Error code */
error:
if (log)
fclose(log);
return -1;
}
}
return 0;
}
Being more specific, every time I use sprintf to make the variable substitution I get the following error: "errno: No such file or directory" whereas using hard-coded fopen filename executes the program correctly.
As far I see the string "route_name" is identical to ../script/dummy0.log so, why is this program complaining once I'm using variable substitution?
Any help is greatly appreciated.