c - strcpy 清除所有文件路径
问题描述
我正在尝试以这种方式将文件的名称和大小保存在目录中:
对于第一个文件,一切都很好。在第二个文件上,它说它无法确定文件路径。这是因为最后我清除了“mainPath”,但是当重新分配发生时,变量“path”也被清除了。
if ((directory = opendir(path)) != NULL)
{
while((d = readdir(directory)) != NULL)
{
if(strcmp(d->d_name, "..")!=0 && strcmp(d->d_name, ".")!=0)
{
strcpy(fileSpec[i].fileName, d->d_name);
char *mainPath = path;
strcat(mainPath, d->d_name);
fileSpec[i].fileSize = stat_filesize(mainPath);
printf("\t%s ---> %ld\n", fileSpec[i].fileName, fileSpec[i].fileSize);
i++;
strcpy(mainPath, "");
}
}
closedir(directory);
}
有一点我不明白。为什么当我strcpy(mainPath, "");
只使用变量“mainPath”时没有清理?
这是因为我需要将路径作为参数传递。
解决方案
推荐阅读
- chef-infra - 如何在 Chef's Test Kitchen 中为 virtualbox vm 设置磁盘大小
- python-3.x - 大 O 符号 | 时间复杂度 - 带有“and”关键字的嵌套循环
- javascript - 为什么悬停在我的 d3 地图上无法正常工作?
- python - pyqt信号在python中的使用
- ruby - 创建linux服务时mysql2出现Ruby on rails问题
- html - 如何将 div 向上移动以在 CSS 中显示另一个 div?
- python - 我正在尝试使用 OpenCV 的项目,但无法克服此错误
- highcharts - Highcharts 隐藏多个系列的空栏类别
- python - Python:创建具有两个以上可选参数的对象
- python - 使用 Altair 直接标记线图