在 C 中仅显示 client/server 中文件夹的部分内容
Show only some contents of a folder in a client/server in C
我正在用 C 开发一个 client/server 程序,我想在其中查看文件夹中的文件内容。该程序有效,但我只会看到一个 .txt 文件,而不是所有文件。我怎样才能做到这一点?谢谢!
DIR *dp;
int rv,rv1,stop_received,nread2;
struct dirent *ep;
char buffer[300],appoggio[1000],buffer2[300];
dp = opendir ("./");
if (dp != NULL){
while (ep = readdir(dp)){
if ((strcmp(ep->d_name, ".") == 0)
|| (strcmp(ep->d_name,"..") == 0)
|| (strcmp(ep->d_name, "SERVERD.c[=11=]") == 0)
||(strcmp(ep->d_name, "h") == 0)
|| (strcmp(ep->d_name, "Menù segreteria") == 0)
||(strcmp(ep->d_name, "Menù docente") == 0))
continue;
strcpy(buffer,ep->d_name);
strcat(buffer,"\n");
send(conn_fd, buffer,strlen(buffer), 0);
}
(void) closedir(dp);
}else
perror ("Couldn't open the directory");
只需在调用 `continue 的条件中添加另一个条件:
|| ((4 <= strlen(ep->d_name)) && (strstr(ep->d_name, ".txt") == (ep->d_name + strlen(ep->d_name) - 4)))
注:4
是由".txt"
中的字符数推导出来的。
注意^2:如果上述测试的编码方式远非高效。例如,在同一个 "string" 上调用 strlen()
两次并不好。
我正在用 C 开发一个 client/server 程序,我想在其中查看文件夹中的文件内容。该程序有效,但我只会看到一个 .txt 文件,而不是所有文件。我怎样才能做到这一点?谢谢!
DIR *dp;
int rv,rv1,stop_received,nread2;
struct dirent *ep;
char buffer[300],appoggio[1000],buffer2[300];
dp = opendir ("./");
if (dp != NULL){
while (ep = readdir(dp)){
if ((strcmp(ep->d_name, ".") == 0)
|| (strcmp(ep->d_name,"..") == 0)
|| (strcmp(ep->d_name, "SERVERD.c[=11=]") == 0)
||(strcmp(ep->d_name, "h") == 0)
|| (strcmp(ep->d_name, "Menù segreteria") == 0)
||(strcmp(ep->d_name, "Menù docente") == 0))
continue;
strcpy(buffer,ep->d_name);
strcat(buffer,"\n");
send(conn_fd, buffer,strlen(buffer), 0);
}
(void) closedir(dp);
}else
perror ("Couldn't open the directory");
只需在调用 `continue 的条件中添加另一个条件:
|| ((4 <= strlen(ep->d_name)) && (strstr(ep->d_name, ".txt") == (ep->d_name + strlen(ep->d_name) - 4)))
注:4
是由".txt"
中的字符数推导出来的。
注意^2:如果上述测试的编码方式远非高效。例如,在同一个 "string" 上调用 strlen()
两次并不好。