Thread: c program find all files in c drive

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    10

    c program find all files in c drive

    i whant to program that can find all files in c:\ folder , the idea is to using linked list to store the folders and subfolders name , than using a fonction that will get the files in those folders

    i have made a floder d with some subfolders in c:\ for test , when i run the program i get some symboles in floders name , any idea to how fixt it?

    Target file is C:\d4☺oj♦
    Target file is C:\d\k\
    Target file is C:\d\pol♦
    Target file is C:\d\volo\


    the code is:
    #include <windows.h> #include <tchar.h> #include <stdio.h> #include <string - Pastebin.com
    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <string.h>
    
    WIN32_FIND_DATA FindFileData;
       HANDLE hFind;
    void files(HANDLE hFind,TCHAR r[]);
    void onlyfiles(HANDLE hFind,TCHAR r[]);
    
      TCHAR pd[]="C:\\d\\";
      TCHAR z[]="*.*";
    TCHAR   r[];
    TCHAR   data[];
    
     struct node{
    
         
        struct node *link;
    	char folder[];
      };
    struct node *start = NULL;//initialiser au null
    struct node *newnode;
    void insertatend(struct node *start,char *data){
         struct node *p,*temp;
         temp=(struct node *)malloc(sizeof(struct node));
    
    
    	strcpy(temp->folder,data);
         p=start;
        while(p->link!=NULL)
               p=p->link;
        p->link=temp;
        temp->link=NULL;
    }
    
    void displaylist(struct node *start){
     
    struct node *p;
    if(start==NULL)
    {
      printf("list is empty\n");
      return;
    }
    
    p=start->link;
    
    while(p!=NULL)
    {          
            
    		  _tprintf (TEXT("Target file is %s\n"), p->folder);
    		//strcpy(r,p->folder);
    		//strcat(r, "\\*.*");
    	//onlyfiles(hFind,r);
    	
    	
        p=p->link;
    
    		
    }
    
    	
    printf("\n");
    }//end of displaylist()
    
    void _tmain()
    
    {
    	strcpy(data,pd);
      
    
    newnode=malloc(sizeof(struct node));
    
    	strcpy(newnode->folder,pd);
    newnode->link=NULL;
    start=newnode;
    
    strcat(r, pd);
    strcat(r, z);
    
     files(hFind,r);
    	FindClose(hFind);
    	displaylist(start);
    	
    }
    void files(HANDLE hFind,TCHAR r[]){
    
                  
    
    hFind = FindFirstFile(r, &FindFileData);
       if (hFind == INVALID_HANDLE_VALUE) 
       {
          printf ("FindFirstFile failed (%d)\n", GetLastError());
          return;
       } 
    if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0){
    _tprintf (TEXT("The first file found is %s\n"), 
                    FindFileData.cFileName);
         }
      
      while(FindNextFile(hFind, &FindFileData) != 0){
    		if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0){
    			
    		strcat(data, FindFileData.cFileName);
    		strcat(data, "\\");
    	
    	  insertatend(start,data);
    			strcpy(data,pd);
    	  
           
    	
    		}
         
    		
    	
    	
    	    }
    }
    void onlyfiles(HANDLE hFind,TCHAR r[]){
    _tprintf (TEXT("The first file found is %s\n"), 
                    r);
    
    hFind = FindFirstFile(r, &FindFileData);
       if (hFind == INVALID_HANDLE_VALUE) 
       {
          printf ("FindFirstFile failed (%d)\n", GetLastError());
          return;
       } 
          if(strcmp(FindFileData.cFileName, ".") != 0 &&
                strcmp(FindFileData.cFileName, "..") != 0){
    _tprintf (TEXT("The first file found is %s\n"), 
                    FindFileData.cFileName);
         }
      
      while(FindNextFile(hFind, &FindFileData) != 0){
    		if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0){
    			
    		             _tprintf (TEXT("The first file found is %s\n"), 
                    FindFileData.cFileName);
           
    	
    		}
     
    
        
    
    	}
    	
    	
    	    }
    Last edited by Salem; 10-14-2016 at 11:00 AM. Reason: Inlined pastebin lazyness

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    1. Post your code here. Be sure to use code tags.
    2. Indent and space your code properly if you expect others to read it.
    3. It's better to use spaces instead of tabs. You should be able to set your editor to insert 4 spaces instead of a tab character.
    4. Break long lines into multiple lines.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    My dog can indent code better than that!

    > char folder[];
    This doesn't magically create a char array which expands to whatever it needs to be when you try and strcpy to it.

    You need to get the length of the string, then adjust the size of the malloc parameter accordingly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    ok i will try it
    thank u

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    i have do that modification in insertatend fubction
    temp=(struct node *)malloc(sizeof(struct node)*sizeof(struct node)); the error is gone from 3 output , their is one error
    Target file is C:\d4☺[jolo
    Target file is C:\d\k\
    Target file is C:\d\polo\
    Target file is C:\d\volo\

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sizeof(struct node)*sizeof(struct node)
    Please explain how this measures the length of a string.

    Sure, it makes the allocation a bit bigger, which is why some short strings are stored OK.

    But that's not the point is it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    any solution ?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you call strlen on the string you want to copy first, then call malloc .
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    i have try it
    len = strlen(data);
    temp=(struct node *)calloc(1,len);
    but i get the same error in the first folder name

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Instead of trying random things, stop and think for a moment. You need space for the node AND the string, including the null-character at the end (which strlen doesn't count).

  11. #11
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    algorism i have also do that but it dont work , i dont think is about size but i have make a directory with a big name , and his ouput was without error , the error always in the first folder name even if it is the smalest name
    len = strlen(data);
    temp=(struct node *)calloc(1,len+sizeof(struct node)+1);

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Are you properly zero-terminating the string?

  13. #13
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    i dont know , the string is get from a function FindNextFile

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps post your latest formatted code.
    Indent style - Wikipedia

    > temp=(struct node *)calloc(1,len+sizeof(struct node)+1);
    This is about right - given the code you have posted so far.

    But you also have some other mistakes regarding char arrays declared with empty sizes as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    pelase tell me were is the mistakes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. burner app can't find right drive
    By joelsmit in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2007, 04:36 PM
  2. Software to find damaged hard drive segments?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 11-25-2004, 02:49 PM

Tags for this Thread