如何使用c程序列出文件夹中的文本文件和html文件? [英] How to list text files and html files in a folder using c program?

查看:59
本文介绍了如何使用c程序列出文件夹中的文本文件和html文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何使用if语句在文件夹或目录中显示所有文本文件和html文件?我正在使用C代码来开发这个,有人可以帮我吗?



这是我开发的示例代码。



Hi,

How can i display all the text files and html files in a folder or a directory using if statement? I am using C code for developing this, could someone help me on this?.

This is a sample code i have developed.

int main(void)
{
    DIR *d;
    struct dirent *dir;
    d = opendir("/home/sosdt009/database/assigncodes/pure c code");
    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
	    if(dir=".txt")
		{
            printf("%s\n", dir->d_name);
	    }
	   else if (dir = ".html")
		{
		  printf("%s\n", dir->d_name);
		 }
	   else 
		{
        	printf( "NO file\n" );     /* Executed if no other statement is */
    		}
	}
        closedir(d);
    }
    return(0);
}

推荐答案

可能会有效:



Probably, this would work:

#include <stdio.h>
#include <string.h>
#include <dir.h>

int main()
{
    struct ffblk f_info;
    int finish = findfirst("*.exe", &f_info, FA_RDONLY| FA_ARCH);
    for(; finish == 0; finish = findnext(&f_info));
    {
        char* extension = NULL;
        if ((!(f_info.ff_attrib & FA_DIREC)) &&
            (extension = strchr(f_info.ff_name,'.') + 1) != NULL)
        {
            if (!strcmp(extension,"txt") || !strcmp(extension,"html"))
                printf("%s\n",f_info.ff_name);
        }       
    }
}</dir.h></string.h></stdio.h>





这是链接 [^] 到findfirst,findnext函数文档



这就是全部。如果有效,请告诉我。



Here's the link[^] to the findfirst, findnext functions documentation

That's all. If it works, just let me know.


这篇关于如何使用c程序列出文件夹中的文本文件和html文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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