Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need a suggestion in searching a file

Need a suggestion in searching a file

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmshelp
13 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Sorry Sir, I think I have not explained my question correctly.I am looking for function which gets the string(will be equal irrespective of the file size) of all files present in the computer and compares with the string which I have defined in my program.More precisely I am looking for a program which searches for the "strings" irrespective of their names.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    Yes I understand your question. And the way to do it, is to get the list of all files in your target set, and then read each file in turn in order to find the string. You can use some the strXXX functions[^] to speed up the searching.

    L 1 Reply Last reply
    0
    • L Lost User

      Yes I understand your question. And the way to do it, is to get the list of all files in your target set, and then read each file in turn in order to find the string. You can use some the strXXX functions[^] to speed up the searching.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #5

      Sir, What if I do if I run the executable on another computer.I have only "C:\\ and D:\\" drives if I run the executable on another computer which has "C:\\ D:\\ E:\\ " drives my code ultimately fails :( .How can I get the list of all files on the computer on which my executable runs.I sincerely thank you for giving me the excellent link for speed up my searching.

      L D 2 Replies Last reply
      0
      • L Lost User

        Sir, What if I do if I run the executable on another computer.I have only "C:\\ and D:\\" drives if I run the executable on another computer which has "C:\\ D:\\ E:\\ " drives my code ultimately fails :( .How can I get the list of all files on the computer on which my executable runs.I sincerely thank you for giving me the excellent link for speed up my searching.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #6

        You really need to learn how to do some more research for yourself. How do you get a list of all the drives? Type "Windows list drives C++" into Google and start reading.

        L 1 Reply Last reply
        0
        • L Lost User

          You really need to learn how to do some more research for yourself. How do you get a list of all the drives? Type "Windows list drives C++" into Google and start reading.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #7

          Yes, this ("Windows list drives C++") didn't strike to my mind. Thank you sir for your kind help.

          1 Reply Last reply
          0
          • L Lost User

            Sir, What if I do if I run the executable on another computer.I have only "C:\\ and D:\\" drives if I run the executable on another computer which has "C:\\ D:\\ E:\\ " drives my code ultimately fails :( .How can I get the list of all files on the computer on which my executable runs.I sincerely thank you for giving me the excellent link for speed up my searching.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #8

            Are you referring to GetLogicalDrives() or GetLogicalDriveStrings()?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            L 1 Reply Last reply
            0
            • D David Crow

              Are you referring to GetLogicalDrives() or GetLogicalDriveStrings()?

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              Thank you Sir for your kind help. Can you please suggest me some links related to this? I am searching in google for something related to this(like getting the strings form an executable) but I cannot find one.

              L 1 Reply Last reply
              0
              • L Lost User

                Thank you Sir for your kind help. Can you please suggest me some links related to this? I am searching in google for something related to this(like getting the strings form an executable) but I cannot find one.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #10

                Why do you keep asking this? I have explained what you need to do to find strings in a file, it's now time to go and try it.

                L 1 Reply Last reply
                0
                • L Lost User

                  Why do you keep asking this? I have explained what you need to do to find strings in a file, it's now time to go and try it.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  Sir, I have tried a lot and finally figured out something. It is by searching for a specific file with MD5 hashes. Since, I want to search for a specific file through the whole drive. Here is my try

                  inline std::string narrow(std::wstring const& text)
                  {
                  std::locale const loc("");
                  wchar_t const* from = text.c_str();
                  std::size_t const len = text.size();
                  std::vector buffer(len + 1);
                  std::use_facet >(loc).narrow(from, from + len, '_', &buffer[0]);
                  return std::string(&buffer[0], &buffer[len]);
                  }

                  void _tmain(int argc, TCHAR *argv[])
                  {

                  HANDLE hFind;
                  WIN32\_FIND\_DATA data;
                  
                  std::string path = "C:\\\\";
                  std::string fileName = "";
                  wchar\_t\* file = L"C:\\\\\*";
                  hFind = FindFirstFile(file, &data);
                  if (hFind != INVALID\_HANDLE\_VALUE) {
                  	do {
                  
                  		fileName = narrow(data.cFileName);
                  
                  		if (fileName.compare(".") == 0)
                  			continue;
                  		if (fileName.compare("..") == 0)
                  			continue;
                  
                  		cout << "FileName: " << fileName << " MD5 hash: ";
                  		// prepare filename
                  		std::string finalFile = path + fileName;
                  		char \*cstr = new char\[finalFile.length() + 1\];
                  		strcpy(cstr, finalFile.c\_str());
                  
                  		//calc the md5 hash
                  		char \*\*temp = CALL\_MD5\_Function(cstr);
                  		cout << temp;
                  		cout <
                  

                  I don't know how to iterate if I found a directory, in here

                  if (fileName.compare(".") == 0)
                  continue;
                  if (fileName.compare("..") == 0)
                  continue;

                  I used continue statement to break the skip the operation since I am unsure. Could you please suggest me some ways to do it?

                  Thank you sir for your kind help

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Sir, I have tried a lot and finally figured out something. It is by searching for a specific file with MD5 hashes. Since, I want to search for a specific file through the whole drive. Here is my try

                    inline std::string narrow(std::wstring const& text)
                    {
                    std::locale const loc("");
                    wchar_t const* from = text.c_str();
                    std::size_t const len = text.size();
                    std::vector buffer(len + 1);
                    std::use_facet >(loc).narrow(from, from + len, '_', &buffer[0]);
                    return std::string(&buffer[0], &buffer[len]);
                    }

                    void _tmain(int argc, TCHAR *argv[])
                    {

                    HANDLE hFind;
                    WIN32\_FIND\_DATA data;
                    
                    std::string path = "C:\\\\";
                    std::string fileName = "";
                    wchar\_t\* file = L"C:\\\\\*";
                    hFind = FindFirstFile(file, &data);
                    if (hFind != INVALID\_HANDLE\_VALUE) {
                    	do {
                    
                    		fileName = narrow(data.cFileName);
                    
                    		if (fileName.compare(".") == 0)
                    			continue;
                    		if (fileName.compare("..") == 0)
                    			continue;
                    
                    		cout << "FileName: " << fileName << " MD5 hash: ";
                    		// prepare filename
                    		std::string finalFile = path + fileName;
                    		char \*cstr = new char\[finalFile.length() + 1\];
                    		strcpy(cstr, finalFile.c\_str());
                    
                    		//calc the md5 hash
                    		char \*\*temp = CALL\_MD5\_Function(cstr);
                    		cout << temp;
                    		cout <
                    

                    I don't know how to iterate if I found a directory, in here

                    if (fileName.compare(".") == 0)
                    continue;
                    if (fileName.compare("..") == 0)
                    continue;

                    I used continue statement to break the skip the operation since I am unsure. Could you please suggest me some ways to do it?

                    Thank you sir for your kind help

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #12

                    You should move your find processing to a separate function and call that with the root path you wish to search. You can then call that recursively each time you find a directory, something like:

                    void ProcessFiles(string root, string directory)
                    {
                    string searchPath = // root plus directory plus mask
                    // start the find loop
                    // if next filename = "." or ".." continue
                    // if next entry is a directory
                    // ProcessFiles(searchPath, directory.name);
                    // ...
                    }
                    int main()
                    {
                    ProcessFiles("C:\\", NULL);
                    }

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      You should move your find processing to a separate function and call that with the root path you wish to search. You can then call that recursively each time you find a directory, something like:

                      void ProcessFiles(string root, string directory)
                      {
                      string searchPath = // root plus directory plus mask
                      // start the find loop
                      // if next filename = "." or ".." continue
                      // if next entry is a directory
                      // ProcessFiles(searchPath, directory.name);
                      // ...
                      }
                      int main()
                      {
                      ProcessFiles("C:\\", NULL);
                      }

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #13

                      Thank you sir for your kind help and I am really sorry for the late reply

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups