请高手看看这段代码为什么打开文件失败!!!!!
char buf[1024];
const char* pathname;
struct stat file_info;
//如果打开文件失败,则试着从当前目录打开
ev->file_fd = open( ev->request, O_RDONLY );
log->debug(log,__FILE__ ,__LINE__,__FUNCTION__,"try to open file:%s",ev->request);
if( ev->file_fd == ERROR ){
if(getcwd(buf, sizeof(buf)-1) == NULL)
{
perror("Fail to get current directory");
return ERROR;
}
strcat(buf,"/");
strcat(buf,ev->request);
log->debug(log,__FILE__ ,__LINE__,__FUNCTION__,"try to open file:%s",buf);
pathname = (char*) malloc(strlen(buf));
strncpy(pathname,buf,strlen(buf));
ev->file_fd = open(pathname,O_RDONLY);
if( ev->file_fd == ERROR ){
perror(buf);
log->debug(log,__FILE__ ,__LINE__,__FUNCTION__,"open file failed");
return ERROR;
}
}
标红的两处总是报:No such file or directory.
问题是我从打出的日志中取出问文件路径,比如:/root/1.txt,直接写在open函数中就ok.
但是我换成变量,就报错.通过后边的perror也可以看到文件路径是这确的呀.
请高手指点为什么会找不到文件呢??