Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
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);
}
Posted
Updated 22-Dec-15 19:55pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Dec-15 1:12am    
How to query a database using switch statement? How print a picture using try-catch block? :-)
If you think it's not funny, re-read the title of your question...
—SA
venkat28vk 23-Dec-15 1:58am    
@ Sergey. I am sorry for the above. The main objective is to list text files and html files in a folder using C code. How can i achieve the above?
Sergey Alexandrovich Kryukov 23-Dec-15 2:39am    
Please, no need to be sorry. You just need to present problems clearly. On step at a time. First, you need to tag your platform. Is it Linux? Then you need to explain what "display" do you need? Just file names, or content. If content... HTML files are "displayed" (rendered) by a Web browser. So, what do you want to do with them? What's the problem, exactly?

If the behavior of your code does not match what you expected, use the debugger and fix it. If you failed to do so, describe what did you want to achieve, what's expected behavior, what is observed behavior, why do you feel it's wrong. If you have some exceptions, describe steps to reproduce, complete exception information. Comment the points in code related to exception throwing/propagation. Post all what's relevant. See also: http://www.sscce.org.

—SA
venkat28vk 23-Dec-15 3:45am    
@sergey. I am using Ubunutu. I need to display only file names. The files will be sent from php to C using tcp/ip, i need to display only the file names of html and text files. Can you give me some code on this.
Sergey Alexandrovich Kryukov 23-Dec-15 11:32am    
Now, what problems did you face? Please see the comment by Richard MacCutchan below, fix this point...
—SA

1 solution

Probably, this would work:

C++
#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>


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

That's all. If it works, just let me know.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900