只显示文件夹的一些内容中在C的客户机/服务器 [英] Show only some contents of a folder in a client/server in C

查看:104
本文介绍了只显示文件夹的一些内容中在C的客户机/服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发在C客户端/服务器程序,我希望看到一个文件夹中的文件内容。该项目工程,但我只看到一个.txt文件,不是所有的文件。我怎样才能做到这一点?谢谢!

  DIR * DP;
INT RV,RV1,stop_received,nread2;
结构的dirent * EP;
炭缓冲器[300],appoggio [1000],缓冲器2 [300];
        DP =执行opendir(./);
        如果(DP!= NULL){
            而(EP = READDIR(DP)){
               如果((STRCMP(EP->中d_name,)== 0)
|| (STRCMP(EP-GT&; d_name,..)== 0)
|| (STRCMP(EP-GT&; d_nameSERVERD.c \\ 0)== 0)
||(的strcmp(EP-> d_name,H)== 0)
|| (STRCMP(EP-GT&; d_name菜单segreteria)== 0)
||(STRCMP(EP-GT&; d_name菜单docente)== 0))
继续;
           的strcpy(缓冲区,EP> d_name);
           strcat的(缓冲,\\ n);
           送(conn_fd,缓冲液,strlen的(缓冲液),0);            }
            (无效)closedir(DP);
        }其他
            PERROR(无法打开目录);


解决方案

只需添加另一个条件调用'继续的:

  || ((4'= strlen的(EP-> d_name))及及(的strstr(EP-> d_name.TXT)==(EP-> d_name + strlen的(EP-> d_name) -  4)))

注:TXT的 4 是字符的数目推导出

请注意^ 2的方式上面的测试codeD,如果远远效率。调用的strlen()两个时间上相同的弦,例如是不是很好。

I'm developing a client/server program in C where I want to see the file contents in a folder. The program works, but I would to see only a .txt file, not all the files. How can I do this? Thank you!

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\0") == 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");

解决方案

Just add another condition to the ones that call `continue:

|| ((4 <= strlen(ep->d_name)) && (strstr(ep->d_name, ".txt") == (ep->d_name + strlen(ep->d_name) - 4)))

Note: The 4 is derived from the number of characters in ".txt".

Note^2: The way the above test is coded if far from being efficient. Calling strlen() two time on the same "string" for example is not nice.

这篇关于只显示文件夹的一些内容中在C的客户机/服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆