Looking for a file in the HardDisk

This is a discussion on Looking for a file in the HardDisk within the C Programming forums, part of the General Programming Boards category; Oky, I have to find a file on the harddrive, where ever it is.. I've built a little program but ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Looking for a file in the HardDisk

    Oky, I have to find a file on the harddrive, where ever it is..
    I've built a little program but it doesn't work well.
    On my system (xp pro) it works but it doesn't find the file I'm looking for if I put it in the Document Folder..
    I let some friends to try and on theyre pc it works in a different way, it seems that the program cant's scan for the file in some folders..but that folders are randomly "choosen" on every pc.. I dont know why, because there are no restrictions.

    The file, once executed prints to the dos window
    "i've found the file in the xxxx folder" ( in italian )
    and repeat it for every yyy ( in that case prova.txt ) files that it found.

    The sources are:

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <windows.h> 
    
    //Ricerca di un File by BianConiglio// 
    
    int _look4aFile (char* _strStartPath)  
    { 
       HANDLE _hFileSearch; 
       WIN32_FIND_DATA _hFileSearchData; 
    
       int lastError = 0; 
       char* _currentSearchPattern = (char*) malloc( MAX_PATH ); 
       char* _reservedBuffer; 
    
       if (_currentSearchPattern) 
       { 
          if (!_strStartPath) 
             _strStartPath = "C:\\"; 
    
          strcpy (_currentSearchPattern, _strStartPath); 
          strcat (_currentSearchPattern, "*"); 
    
          if ( (_hFileSearch = FindFirstFile(_currentSearchPattern, &_hFileSearchData)) != INVALID_HANDLE_VALUE ) 
          { 
             do { 
                if ( strcmp(_hFileSearchData.cFileName, ".") && strcmp(_hFileSearchData.cFileName, "..") ) 
                { 
                   if ( _hFileSearchData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ) { 
    
                      _reservedBuffer = (char*) malloc( MAX_PATH+1 ); 
                      if ( _reservedBuffer ) 
                      { 
                         strcpy (_reservedBuffer, _strStartPath); 
                         strcat (_reservedBuffer, _hFileSearchData.cFileName); 
                         strcat (_reservedBuffer, "\\"); 
    
                         lastError = _look4aFile (_reservedBuffer); 
    
                         free (_reservedBuffer); 
    
                      } else 
                         lastError = -1; 
            
                   } else { 
                                          
                      if ( !strcmp(_hFileSearchData.cFileName, "prova.txt" )) 
                      
                      printf ("Ho trovato un file \"%s\" nella cartella \"%s\"\n", _hFileSearchData.cFileName, _strStartPath); 
                        
                     } 
                } 
                  
             } 
        
              while ( FindNextFile(_hFileSearch, &_hFileSearchData) ); 
          } else 
             lastError = 1; 
    
          free (_currentSearchPattern); 
          FindClose (_hFileSearch); 
    
       } else 
          lastError = -1; 
    
       return lastError; 
    } 
    
    
    int main() 
    
    { 
    
      _look4aFile (NULL); 
      
      system("PAUSE");    
      return 0; 
    }
    Can someone tell me WHY it works randomly ??
    In that source i'm looking for the file "prova.txt"

    what I have to change in the source ?

    thanx

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    if ( _hFileSearchData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ) {
    A directory may have other attributes, in which case this will fail. Therefore use a bitmask to test the directory attribute:
    Code:
    if ( _hFileSearchData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
    Code:
    if ( !strcmp(_hFileSearchData.cFileName, "prova.txt" ))
    strcmp() is case sensitive. Therefore this will not find "Prova.txt". Consider using stricmp().

    On a style note, why are your variables and functions names prepended with an underscore?

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    I LOVE you It works !!

    The underscore ?? Mh I don't remember ! Maybe only for fun I did it mounths ago

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Ok now it works but I'm not able to do the next step.

    I want to create a file in the same directory where it is found the default file (prova.txt)

    here it is the Look4aFile i've added commens with "// <---" in capital letters where I added code from the last post. ( only some lines and i'm allready stopped by errors )

    Code:
    int look4aFile (char* strStartPath)  
    { 
       HANDLE hFileSearch; 
       WIN32_FIND_DATA hFileSearchData; 
    
       FILE *userinfo;              //  <-- THE FILE I WANT TO CREATE
       char temp[100]="\0";   //  <-- A STRING WITH THE FULL PATH
       
       int lastError = 0; 
       char* currentSearchPattern = (char*) malloc( MAX_PATH ); 
       char* reservedBuffer; 
    
       if (currentSearchPattern) 
       { 
          if (!strStartPath) 
             strStartPath = "C:\\"; 
    
          strcpy (currentSearchPattern, strStartPath); 
          strcat (currentSearchPattern, "*"); 
    
          if ( (hFileSearch = FindFirstFile(currentSearchPattern, &hFileSearchData)) != INVALID_HANDLE_VALUE ) 
          { 
             do { 
                if ( strcmp(hFileSearchData.cFileName, ".") && strcmp(hFileSearchData.cFileName, "..") ) 
                { 
                   if ( hFileSearchData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { 
    
                      reservedBuffer = (char*) malloc( MAX_PATH+1 ); 
                      if ( reservedBuffer ) 
                      { 
                         strcpy (reservedBuffer, strStartPath); 
                         strcat (reservedBuffer, hFileSearchData.cFileName); 
                         strcat (reservedBuffer, "\\"); 
    
                         lastError = look4aFile (reservedBuffer); 
    
                         free (reservedBuffer); 
    
                      } else 
                         lastError = -1; 
            
                   } else { 
                                          
                      if ( !stricmp(hFileSearchData.cFileName, "prova.txt" )) 
                      
                     printf ("Ho trovato un file \"%s\" nella cartella \"%s\"\n", hFileSearchData.cFileName, strStartPath); 
            
                    strcpy (temp,strStartPath);  //  <-- I COPY THE PATH FOUND
                    strcat (temp,"\\user_info.sah");  //  <-- I ADD TO THE PATH THE NAME OF THE FILE I WANT TO CREATE
                    userinfo = fopen(temp, "wb"); // <-- I CREATE THE EMPTY FILE
                    fclose(userinfo); // <-- CLOSED                               
                            
                     } 
                } 
                  
             } 
        
              while ( FindNextFile(hFileSearch, &hFileSearchData) ); 
          } else 
             lastError = 1; 
    
          free (currentSearchPattern); 
          FindClose (hFileSearch); 
    
       } else 
          lastError = -1; 
    
       return lastError; 
    }
    IT doesnt work and i need help for 2 things :

    1st It crushes i think i did something wrong with the string..maybe..

    2nd I have to clean the "temp" string because it is a "do--while" and i can find more than one "prova.txt" and i want to create more than one "user_info.sah" so the temp string has to be celan for the next time

    Thank u everyone for an hand
    Last edited by BianConiglio; 03-15-2004 at 08:25 AM.

  5. #5
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Not sure if this is the problem, but when you say:

    char temp[100]="\0";

    that is making a dangerous assumtion that the file path will only be 100 chars long. If you are using windows then you can use the constant MAX_PATH here instead which should be safe, although I'm not sure if there would be problems if ms decided to change the maximum chars allowed in a path. If you are using a different OS there should be some other way of retriving the maximum path, but you'll have to look it up yourself. Anyway, that's all I have time to look at now, but I hope it helps.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If you are using windows then you can use the constant MAX_PATH here instead which should be safe
    Or FILENAME_MAX if you don't mind a standard C solution.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thank u everyone ! it works !!

    now i have a quite small problem...

    ok it works but...if I execute it, it looks for "prova.txt" and creates "user_info.sah" in every folder where prova.txt it is found.....

    then I shut down all, I delete all (prova.txt and user_info.sah) and execute again my file...he says me no prova.txt found and it's ok..BUT if I look in the folder where there WERE BEFORE prova.txt, I found recreated the files user_info.sah !!! ( but no prova.txt coz i've deleted it before!! )

    It seems that there is a sort of "memory" of the OLD positions (the strStartPath) of the files.

    Do I have to call ghostbusters for that ???
    What do u think ?

    PS: great forum with great ppl, finally i've found a nice place !!

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by BianConiglio
    thank u everyone ! it works !!

    now i have a quite small problem...

    ok it works but...if I execute it, it looks for "prova.txt" and creates "user_info.sah" in every folder where prova.txt it is found.....

    then I shut down all, I delete all (prova.txt and user_info.sah) and execute again my file...he says me no prova.txt found and it's ok..BUT if I look in the folder where there WERE BEFORE prova.txt, I found recreated the files user_info.sah !!! ( but no prova.txt coz i've deleted it before!! )

    It seems that there is a sort of "memory" of the OLD positions (the strStartPath) of the files.

    Do I have to call ghostbusters for that ???
    What do u think ?

    PS: great forum with great ppl, finally i've found a nice place !!
    Look at
    Code:
                      if ( !stricmp(hFileSearchData.cFileName, "prova.txt" )) 
                      
                     printf ("Ho trovato un file \"%s\" nella cartella \"%s\"\n", hFileSearchData.cFileName, strStartPath); 
            
                    strcpy (temp,strStartPath);  //  <-- I COPY THE PATH FOUND
                    strcat (temp,"\\user_info.sah");  //  <-- I ADD TO THE PATH THE NAME OF THE FILE I WANT TO CREATE
                    userinfo = fopen(temp, "wb"); // <-- I CREATE THE EMPTY FILE
                    fclose(userinfo); // <-- CLOSED
    The if() statement prints the message only if you found the file. All of the other stuff is executed whether you found the file or not!

    use if() {
    //stuff
    //stuff
    //stuff
    }

    Dave

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Oh, yeah. I didn't know you could do this:
    Code:
    strStartPath = "C:\\";
    I thought you had to use strcpy().

    Dave

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I thought you had to use strcpy().
    Not if strStartPath is a pointer. Which is the case since it is a function parameter. As long as the string is not modified, all will be well.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by Prelude
    >I thought you had to use strcpy().
    Not if strStartPath is a pointer. Which is the case since it is a function parameter. As long as the string is not modified, all will be well.
    Thanks, again, for useful knowledge.

    Dave

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    ok i've put the

    Code:
    if
    {
    bla bla bla
    }
    and now it works...i was stupid I havent' thought about that !!

    anyway now i'v found that i've created an user_info.sah in EVERY DAMN folder of my system lol !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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