![](/img/trans.png)
[英]Pointer array to (with pointer array defined) strings: Are the strings stored sequential in memory?
[英]Using an array of filenames stored as strings
我的程序遍历单个目录(非递归),并将该目录中所有文件的名称存储在数组中。 然后,它在程序的第二部分中使用该数组,并返回有关每个文件的一些信息。 我可以遍历目录,可以处理单个文件,但是在合并程序的两个部分时遇到了麻烦。 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
int getArraySize(char* arr[]);
int getArraySize(char* arr[]) {
return sizeof(&arr);
}
char *filesArray[200];
int main (int argc, char* argv[])
{
DIR *dir;
struct dirent *ent;
int filesCtr = 0;
if ((dir = opendir ("/home/dshah/Documents/CECS 420/Project 3")) != NULL) {
while ((ent = readdir (dir)) != NULL) { /* print all the files and directories within directory */
if (strcmp(ent->d_name, ".") == 0) {
continue;
} else if (strcmp(ent->d_name, "..") == 0) {
continue;
} else if (ent->d_type == 4) { // if a directory
continue;
} else {
filesArray[filesCtr] = ent->d_name;
printf("%s\n", filesArray[filesCtr]);
filesCtr++;
}
}
closedir (dir);
} else { /* could not open directory */
perror ("Could not open directory");
}
int i;
for (i = 0; i < getArraySize(filesArray); i++) {
char* filename = filesArray[i];
FILE *file = fopen (filename, "r");
if (file != NULL) {
char line [128]; /* or other suitable maximum line size */
int ctr = 1;
while (fgets(line, sizeof line, file) != NULL) { /* read a line */
if (strstr(line, "is") != NULL) {
printf("%s:%d:%s", filename, ctr, line);
}
ctr++;
}
fclose (file);
} else {
perror (filename); /* why didn't the file open? */
}
}
return 0;
}
我遇到的问题是:
char* filename = filesArray[i];
这行代码正确吗? 当我将filename
设置为类似于"file.txt"
的字符串时,它可以工作,所以当我执行printf("n %s\\n", filesArray[i]);
时,这也不应该工作吗printf("n %s\\n", filesArray[i]);
? 这行代码中的filesArray[i]
是字符串吗?
编辑:
谢谢,这解决了问题。 另一个快速的问题:我正在尝试将完整路径附加到
FILE *file = fopen (filename, "r");`
通过将其更改为
FILE *file = fopen (strcat("/home/dshah/Documents/CECS 420/Project 3/", filename), "r");
但这给了我一个分割错误。 这项工作不应该导致我仅指定路径吗?
当您将数组传递给函数时,它会衰减为指针,因此当您执行&arr
您实际上会获得指向该指针的指针,而指针的大小很可能不是原始数组的大小。 如果(实际上是如果)数组实际上是一个字符串,则可以使用strlen
来获取字符串的长度(不包括字符串终止符)。
在您的情况下,实际上并不需要 getArraySize
函数,因为您已经有一个计数器来告诉您filesArray
数组中有多少个字符串: filesCtr
变量。
同样,当使用诸如readdir
之类的函数时,返回条目的d_name
字段实际上可能指向静态数组,因此您不能只复制指针,而必须复制完整的字符串。 这是通过strdup
函数完成的:
filesArray[filesCtr] = strdup(ent->d_name);
请记住,完成后必须free
此字符串。
哦,并且避免在代码中使用“幻数”,例如,在检查目录条目是否为子目录( ent->d_type == 4
)时。 使用可用的宏( end->d_type == DT_DIR
)。
最后, readdir
条目的d_name
字段仅包含实际文件名,而不包含完整路径。 因此,如果要使用完整路径,则必须附加路径和文件名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.