get path of filename

This is a discussion on get path of filename within the C Programming forums, part of the General Programming Boards category; hi guys and execuse my english my goal is to do a programme input filename (exemple toto.exe) by user the ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    get path of filename

    hi guys and execuse my english
    my goal is to do a programme input filename (exemple toto.exe) by user the programme
    look for the directory of the file if it exist and stock it into a string.
    i tried to use <dirent.h> and findfirstfile and findnextfile and i fail
    how force the programme scan all hard disk ( c: and d: or f: ..etc)
    thx for any help

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How did you fail? What does your code look like?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    
    
    int main()
    {
        
        
        
    
        
         WIN32_FIND_DATA ffdata;
         HANDLE ffind;
         int i;
    
         ffind=FindFirstFile("*.*", &ffdata);
         printf ("The first file found is %s\n", ffdata.cFileName);
         i=strcmp("lilo3.exe",ffdata.cFileName);
    
           while (FindNextFile(ffind, &ffdata) != 0 && (i!=0)) {
    
    
             FindNextFile(ffind, &ffdata);
             printf("%s",ffdata.cFileName);
             i=strcmp("lilo3.exe",ffdata.cFileName);}
    
    
    
         FindClose(ffind);
    
    
    
         return 0;
    
    
    
    }
    in this try my goal is to find lilo3.exe file


    this is my try with <dirent.h>


    Code:
    #include <stdio.h>
    #include <dirent.h>
    
    int main()
    {
    
    
       struct dirent *seek;
    
    
       DIR *dir;
    
    
       dir = opendir("D:\\");
    
    
    
       while ((seek = readdir(dir)&& (strcmp ( seek->d_name , " lilo3.exe ") != 0)))
       {
    
          
          printf("FICHIER: %s\n", seek->d_name);
    
       }
    
       closedir(dir);
    
    
    
    
    
    return 0;
    
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're only checking the root directory of your drive. If you want to check all the directories, you need to look to see whether the thing seek is pointing to is a directory or not, and if so, enter it and do it all again. Check the CProgramming FAQ on this very site for this question.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    your FindFile version
    1. calls FindFile twice on each iteration
    2. ignores the return value of FindNextFile
    3. uses too general mask
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    ffind=FindFirstFile("*.*", &ffdata);
    If you're looking for a specific filename, why not look for that exact filename?
    Code:
    ffind=FindFirstFile("lilo3.exe", &ffdata);
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Getting just the filename from a path
    By deviousdexter in forum C# Programming
    Replies: 7
    Last Post: 11-26-2008, 12:56 PM
  3. Using string operations to save part of a file path
    By JackR in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2007, 03:48 PM
  4. user defined path for FILENAME
    By drdigimon in forum C Programming
    Replies: 5
    Last Post: 10-12-2003, 06:04 PM
  5. getting a file's path and filename
    By face_master in forum Windows Programming
    Replies: 4
    Last Post: 02-05-2002, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21