我正在运行下面的代码来检查文件是否存在,但是当将字符串传递给stat时,它会返回失败。字符串的值不同,但打印它们显示相同的值
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
struct stat statbuf;
char tmp_buf1[100];
char result [100];
char result1[100]="/root/file.sh";
strcpy(tmp_buf1,"echo $HOME/file.sh");
FILE* fp;
fp = popen(tmp_buf1,"r");
printf("Name passed is:%s\n",tmp_buf1);
fread(result,1,sizeof(result),fp);
fclose (fp);
printf("The full path is %s\n",result);
int rc = 0;
// To find out difference b/w the the strings, I am doing a strcmp, it is returning 10.
int r = strcmp(result,result1);
printf (" Return is = %d\n",r);
rc = stat(result, &statbuf);
if (rc == -1) {
printf("File is NOT HERE!\n");
printf("Return Code = %d",rc);
}
else
printf("Found it !");
}
不知道这些字符串是怎么来的不一样。
你是否以root身份运行?当你printf()'它们(或在调试器中)时,字符串会说什么? –
'fread'返回什么?另外,如果一个函数失败(如'stat'),错误在'errno'中。 –
另一件事,请在问题中包含程序的实际输出。 –