检查是否文件路径是可读写的 [英] Checking if file path is readable and writable
本文介绍了检查是否文件路径是可读写的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想列出的所有文件和子目录递归地给出一个文件路径。这工作,直到我尝试添加code,以检查文件的路径是可读/写的(这是我发表意见的线条勾勒出)。现在不进入递归循环。这是我的code
的#include<&unistd.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&;&dirent.h GT;
#包括LT&;&stdio.h中GT;无效listdir同时(字符*名称,FILE * FP)
{
DIR * DIR;
结构的dirent *进入; 如果(!(DIR =执行opendir(名)))
返回;
如果(!(条目= READDIR(DIR)))
返回; 做{
FILE * fileCopy;
烧焦阅读[50];
写的char [50];
焦炭路径[1024];
INT LEN =的snprintf(路径的sizeof(路径)-1,%S /%S的名字,入门> d_name);
路径[长度] = 0;
如果(入门级GT&; d_type == DT_DIR)
{
如果(STRCMP(入门>中d_name,)== 0 || STRCMP(入门> d_name,..)== 0)
继续;
//如果((fileCopy = FOPEN(路径,RB))== NULL){
//的strcpy(读,不读);
//}
//其他{
//的strcpy(读读);
//}
//如果((fileCopy = FOPEN(路径,世行))== NULL){
//的strcpy(写上不可写);
//}
//其他{
//的strcpy(写,可写);
//}
fprintf中(FP,[D]。%S - %S%S \\ n,路径,读,写);
listdir同时(路径,FP);
}
其他
{
//如果((fileCopy = FOPEN(路径,RB))== NULL){
//的strcpy(读,不读);
//}
//其他{
//的strcpy(读读);
//}
//如果((fileCopy = FOPEN(路径,世行))== NULL){
//的strcpy(写上不可写);
//}
//其他{
//的strcpy(写,可写);
//}
fprintf中(FP[F]%S - %S%S \\ n,路径,读,写);
}
}而((条目= READDIR(DIR)));
closedir(DIR);}INT主要(无效)
{
FILE * FP;
FP = FOPEN(在/ var /移动/应用/ FileIOAccess.txt,W);
listdir同时(/无功,FP);
FCLOSE(FP);
返回0;
}
解决方案
本例使用访问
来替换你的使用的fopen
来测试该文件的权限。
无效listdir同时(字符*名称,FILE * FP)
{
DIR * DIR;
结构的dirent *进入; 如果(!(DIR =执行opendir(名)))
返回; 如果(!(条目= READDIR(DIR)))
返回; 做
{
焦炭readString [50] = {0};
焦炭writeString [50] = {0};
焦炭路径[1024];
字符的文件类型; 的snprintf(路径的sizeof(路径)-1,%S /%S的名字,入门> d_name); 如果(STRCMP(入门>中d_name,)== 0 || STRCMP(入门> d_name,..)== 0)
继续; 如果(访问(路径,R_OK)== 0)
的strcpy(readString,读);
其他
的strcpy(readString,未读); 如果(访问(路径,W_OK)== 0)
的strcpy(writeString,可写);
其他
的strcpy(writeString,不可写); 开关(入门级GT&; d_type)
{
案例DT_UNKNOWN:文件类型=;'?打破;
案例DT_FIFO:文件类型='P';打破;
案例DT_CHR:文件类型='C';打破;
案例DT_DIR:文件类型='D';打破;
案例DT_BLK:文件类型='B';打破;
案例DT_REG:文件类型='F';打破;
案例DT_LNK:文件类型='L';打破;
案例DT_SOCK:文件类型='S';打破;
案例DT_WHT:文件类型='W';打破;
默认:文件类型=;'?打破;
} fprintf中(FP[%C]%S - %S%S \\ n,文件类型,路径,readString,writeString); 如果(入门级GT&; d_type == DT_DIR)
listdir同时(路径,FP); }而((条目= READDIR(DIR))); closedir(DIR);
}
I am trying to list all the files and sub directories recursively given a file path. This works, until when I try to add the code to check if the file path is readable/writeable(which I commented the lines out). It now does not go into the recursive loop. This is my code
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
void listDir(char *name, FILE *fp)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do {
FILE *fileCopy;
char read[50];
char write[50];
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (entry->d_type == DT_DIR)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// if((fileCopy = fopen(path, "rb")) == NULL){
// strcpy(read,"Not Readable");
// }
// else{
// strcpy(read,"Readable");
// }
// if((fileCopy = fopen(path, "wb")) == NULL){
// strcpy(write,"Not Writable");
// }
// else{
// strcpy(write,"Writable");
// }
fprintf(fp,"[D]%s - %s,%s\n", path,read,write);
listDir(path ,fp);
}
else
{
// if((fileCopy = fopen(path, "rb")) == NULL){
// strcpy(read,"Not Readable");
// }
// else{
// strcpy(read,"Readable");
// }
// if((fileCopy = fopen(path, "wb")) == NULL){
// strcpy(write,"Not Writable");
// }
// else{
// strcpy(write,"Writable");
// }
fprintf(fp,"[F]%s - %s,%s\n", path,read,write);
}
} while ((entry = readdir(dir)));
closedir(dir);
}
int main(void)
{
FILE *fp;
fp = fopen("/var/mobile/Applications/FileIOAccess.txt", "w");
listDir("/var",fp);
fclose(fp);
return 0;
}
解决方案
This example uses access
to replace your use of fopen
to test the file permissions.
void listDir(char *name, FILE *fp)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do
{
char readString[50] = {0};
char writeString[50] = {0};
char path[1024];
char filetype;
snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if (access(path, R_OK) == 0)
strcpy(readString, "Readable");
else
strcpy(readString, "Not Readable");
if (access(path, W_OK) == 0)
strcpy(writeString, "Writable");
else
strcpy(writeString, "Not Writable");
switch (entry->d_type)
{
case DT_UNKNOWN: filetype = '?'; break;
case DT_FIFO: filetype = 'P'; break;
case DT_CHR: filetype = 'C'; break;
case DT_DIR: filetype = 'D'; break;
case DT_BLK: filetype = 'B'; break;
case DT_REG: filetype = 'F'; break;
case DT_LNK: filetype = 'L'; break;
case DT_SOCK: filetype = 'S'; break;
case DT_WHT: filetype = 'W'; break;
default: filetype = '?'; break;
}
fprintf(fp,"[%c]%s - %s,%s\n", filetype, path, readString, writeString);
if (entry->d_type == DT_DIR)
listDir(path, fp);
} while ((entry = readdir(dir)));
closedir(dir);
}
这篇关于检查是否文件路径是可读写的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文