C fopen:文件不存在,但确实存在
我想读取可执行文件所在文件夹中的所有文件,但我正在运行的可运行文件除外。我编写了以下代码,但是,尽管此列表正确列出了我文件夹中的文件,但我无法使用fopen打开它们,因为fopen打印的文件不存在。如果我使用gedit“从我的c程序中获得的文件的路径”,那么它将从术语中完全打开。臭虫在哪里C fopen:文件不存在,但确实存在,c,file,unix,fopen,C,File,Unix,Fopen,我想读取可执行文件所在文件夹中的所有文件,但我正在运行的可运行文件除外。我编写了以下代码,但是,尽管此列表正确列出了我文件夹中的文件,但我无法使用fopen打开它们,因为fopen打印的文件不存在。如果我使用gedit“从我的c程序中获得的文件的路径”,那么它将从术语中完全打开。臭虫在哪里 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
int main (int argc, char **argv) {
//Determining the number of files we have.
//We call to a bash command http://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output
FILE *fp, *fin;
char path[1035], cwd[1024];
int scanned = 0;
/* Open the command for reading. */
//https://askubuntu.com/questions/370697/how-to-count-number-of-files-in-a-directory-but-not-recursively
//This count soft and hard links also (I think)
fp = popen("ls -F |grep -v /", "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
/* Read the output a line at a time - output it. */
//Loop for each file. Be careful! if the exe is inside, it will also be counted!
while (fgets(path, sizeof(path)-1, fp) != NULL) {
printf("Reading file: %s\n", path);
fin=fopen(path,"r");
scanned = 0;
printf("caa");
if (fin != NULL){
printf("AA\n");
fclose(fin);
}
if (!fin)perror("fopen");
printf("Done! \n");
}
/* close */
pclose(fp);
printf("end");
return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
//确定我们拥有的文件数量。
//我们调用bash命令http://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output
文件*fp,*fin;
字符路径[1035],cwd[1024];
int=0;
/*打开命令进行读取*/
//https://askubuntu.com/questions/370697/how-to-count-number-of-files-in-a-directory-but-not-recursively
//这也包括软链接和硬链接(我认为)
fp=popen(“ls-F | grep-v/”,“r”);
如果(fp==NULL){
printf(“无法运行命令\n”);
出口(1);
}
/*一次读取一行输出-输出它*/
//循环每个文件。小心!如果exe在里面,它也会被计算在内!
while(fgets(path,sizeof(path)-1,fp)!=NULL){
printf(“读取文件:%s\n”,路径);
fin=fopen(路径“r”);
扫描=0;
printf(“caa”);
如果(fin!=NULL){
printf(“AA\n”);
财务总监(财务);
}
如果(!fin)perror(“fopen”);
printf(“完成!\n”);
}
/*接近*/
pclose(fp);
printf(“结束”);
返回0;
}
您的代码中有两个错误:
while (fgets(path, sizeof(path)-1, fp) != NULL) {
len=strlen(path);
path[len-1]='\0';
while (fgets(path, sizeof(path)-1, fp) != NULL) {
len=strlen(path);
path[len-1]='\0';
fp=popen(“ls-A1 | grep-v/”,“r”)代码>
您的代码中有两个错误:
while (fgets(path, sizeof(path)-1, fp) != NULL) {
len=strlen(path);
path[len-1]='\0';
while (fgets(path, sizeof(path)-1, fp) != NULL) {
len=strlen(path);
path[len-1]='\0';
fp=popen(“ls-A1 | grep-v/”,“r”)代码>
好的,以防其他人需要更好的方法,我用我的注释重新编辑了代码。在这里,我给你新的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
int isDirectory(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != 0)
return 0;
return S_ISDIR(statbuf.st_mode);
}
int main (int argc, char **argv) {
FILE *fp, *fin;
char path[1035], cwd[1024];
int scanned = 0;
int ints;
DIR *dir;
struct dirent *ent;
//getcwd prints directory where the app ran.
if ((dir = opendir (getcwd(cwd, sizeof(cwd)))) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
/*Skips . and ..*/
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
if (isDirectory(ent->d_name) != 0) continue;
printf ("Reading file: %s\n", ent->d_name);
scanned = 0;
fin=fopen(ent->d_name,"r");
if (fin != NULL){
while ((scanned = fscanf(fin, "%d", ints)) != EOF) {
if(scanned == 1){
printf("%d\n", ints);
}else {
printf("Whoops! Input format is incorrect!\n");
break;
}
} //LOOP: reading file
fclose(fin);
}
if (!fin)perror("fopen");
printf("Done! \n");
}//LOOP: while opendir
closedir (dir);
} else {
/* could not open directory */
perror ("opendir");
return EXIT_FAILURE;
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int isDirectory(常量字符*路径){
结构stat statbuf;
if(stat(path和statbuf)!=0)
返回0;
返回S_ISDIR(statbuf.st_模式);
}
int main(int argc,字符**argv){
文件*fp,*fin;
字符路径[1035],cwd[1024];
int=0;
整数整数;
DIR*DIR;
结构导向;
//getcwd打印应用程序运行的目录。
if((dir=opendir(getcwd(cwd,sizeof(cwd)))!=NULL){
/*打印目录中的所有文件和目录*/
while((ent=readdir(dir))!=NULL){
/*跳过,然后*/
如果(strcmp(ent->d_name,“.”)=0 | | strcmp(ent->d_name,“…”)==0)继续;
如果(isDirectory(ent->d_name)!=0)继续;
printf(“正在读取文件:%s\n”,ent->d_名称);
扫描=0;
fin=fopen(ent->d_名称,“r”);
如果(fin!=NULL){
而((扫描=fscanf(fin,“%d”,ints))!=EOF){
如果(扫描==1){
printf(“%d\n”,ints);
}否则{
printf(“哇!输入格式不正确!\n”);
打破
}
}//循环:正在读取文件
财务总监(财务);
}
如果(!fin)perror(“fopen”);
printf(“完成!\n”);
}//循环:而opendir
closedir(dir);
}否则{
/*无法打开目录*/
perror(“opendir”);
返回退出失败;
}
返回0;
}
好的,为了防止其他人需要更好的方法,我用我的注释重新编辑了代码。在这里,我给你新的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
int isDirectory(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != 0)
return 0;
return S_ISDIR(statbuf.st_mode);
}
int main (int argc, char **argv) {
FILE *fp, *fin;
char path[1035], cwd[1024];
int scanned = 0;
int ints;
DIR *dir;
struct dirent *ent;
//getcwd prints directory where the app ran.
if ((dir = opendir (getcwd(cwd, sizeof(cwd)))) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
/*Skips . and ..*/
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
if (isDirectory(ent->d_name) != 0) continue;
printf ("Reading file: %s\n", ent->d_name);
scanned = 0;
fin=fopen(ent->d_name,"r");
if (fin != NULL){
while ((scanned = fscanf(fin, "%d", ints)) != EOF) {
if(scanned == 1){
printf("%d\n", ints);
}else {
printf("Whoops! Input format is incorrect!\n");
break;
}
} //LOOP: reading file
fclose(fin);
}
if (!fin)perror("fopen");
printf("Done! \n");
}//LOOP: while opendir
closedir (dir);
} else {
/* could not open directory */
perror ("opendir");
return EXIT_FAILURE;
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int isDirectory(常量字符*路径){
结构stat statbuf;
if(stat(path和statbuf)!=0)
返回0;
返回S_ISDIR(statbuf.st_模式);
}
int main(int argc,字符**argv){
文件*fp,*fin;
字符路径[1035],cwd[1024];
int=0;
整数整数;
DIR*DIR;
结构导向;
//getcwd打印应用程序运行的目录。
if((dir=opendir(getcwd(cwd,sizeof(cwd)))!=NULL){
/*打印目录中的所有文件和目录*/
while((ent=readdir(dir))!=NULL){
/*跳过,然后*/
如果(strcmp(ent->d_name,“.”)=0 | | strcmp(ent->d_name,“…”)==0)继续;
如果(isDirectory(ent->d_name)!=0)继续;
printf(“正在读取文件:%s\n”,ent->d_名称);
扫描=0;
fin=fopen(ent->d_名称,“r”);
如果(fin!=NULL){
而((扫描=fscanf(fin,“%d”,ints))!=EOF){
如果(扫描==1){
printf(“%d\n”,ints);
}否则{
printf(“哇!输入格式不正确!\n”);
打破
}
}//循环:正在读取文件
财务总监(财务);
}
如果(!fin)perror(“fopen”);
printf(“完成!\n”);
}//循环:而opendir
closedir(dir);
}否则{
/*无法打开目录*/
perror(“opendir”);
返回退出失败;
}
返回0;
}
Tryprintf(“读取文件[%s]\n”,路径)
fopen
不打印任何内容!解析ls
outp