我想扫描一个目录,检查 JPEG 文件(即前几个字节的组合)并将它们复制到另一个驱动器。我成功地测试了该程序并且它可以筛选文档。但是,我无法访问整个目录(例如我不小心删除了 D:/中的 SD 卡)。
以下是我尝试访问它的方法:
// remember the path from the command line
char *path = argv[1];
// open the path (preferably "D:\" - which is my SD-Card ;))
FILE *inptr = fopen(path, "r");
if (inptr == NULL)
{
fprintf(stderr, "inputfile could not be read\n");
return 1;
}
输出是“无法读取输入文件” - 这就是为什么我非常确信错误就在那里。我需要以不同的方式寻址目录吗?例如。通过使用指向驱动器第一位的指针?
我是初学者 - 所以笑的时候请保持温柔。 ;)
非常感谢! 马塞尔
最佳答案
“fopen()”无法打开整个目录或驱动器,它只能打开特定路径。但是,有多种方法可以完成列出目录中所有文件的任务。 This answer已经包含一些相关信息。
由于您已经可以打开文件,因此您可以调整此处显示的循环来执行操作。
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
if (/*Check whether file has the .jpg extension or another test*/)
{
// perform your copy operation on the file
}
}
关于c - 访问c中的不同驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45536526/