dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Strings in VC++ 6

  1. #1
    Join Date
    Feb 2012
    Posts
    16

    Strings in VC++ 6

    Hi

    Im using the old VC++ 6 (for some stupid reasons). Im coding a simple WINAPI call to do a recursive file search using 'FindFirstFile' .

    Im not been able to concatenate two string. In fact Im not been able to use strings at all, Im currently using char* . I want to use the strings.

    when I use : using namespace std; I get the error: error C2871: 'std' : does not exist or is not a namespace


    I want to do a simple file search inside a path, recursively.

    Im posting this problem after scrapping my brain for 2 days ....

    regards
    Raj

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,716

    Re: Strings in VC++ 6

    Did you ...
    Code:
    #include <string>
    
    using namespace std;
    in that order ??

  3. #3
    Join Date
    Feb 2012
    Posts
    16

    Re: Strings in VC++ 6

    Hi

    Thanx , I tried that earlier as well , however I get this

    error C2065: 'string' : undeclared identifier


    ***************************code******************
    #include <string>

    using namespace std;

    ....


    string abc = "abc";
    string xyz;

    xyz = abc + "cde";
    ***************************code******************

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,651

    Re: Strings in VC++ 6

    Where is stdafx.h? Before or after #include <string>?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: Strings in VC++ 6

    Remember also that most of STL was utter c**p in VC++6. std::string might work (maybe) but many other parts of STL either weren't supported or just didn't work.

    Do upgrade your compiler. If you're short of cash you can usually obtain VC++8 (Visual Studio 2005) at very reasonable prices and it's soooo much better than VC++6 in every way.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: Strings in VC++ 6

    Not for "some stupid reasons" but for testing purpose, I still use sometimes Visual C++ 6.0 (Enterprise Edition, SP5) and have no problem.

    I've attached here a little test application using std::string and other STL stuff.
    Just try to compile it yourself. If no success, then it's a mistake in your program or VC6 is not properly installed.
    Attached Files Attached Files
    Last edited by ovidiucucu; February 21st, 2012 at 09:46 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  7. #7
    Join Date
    Feb 2012
    Posts
    16

    Re: Strings in VC++ 6

    Thanx , It worked like charm,

    Also I wanted to find recursive a file inside a path , I tried this ... But i guess its wrongly done.

    void list_folder(string sFolderPtr, list<string>& lFileNames)
    {
    string& sFolder = sFolderPtr;
    string sFind = sFolder + "\\*.txt";
    WIN32_FIND_DATA findData = {0};
    HANDLE hFileFind = ::FindFirstFileA(sFind.c_str(), &findData);
    if(INVALID_HANDLE_VALUE != hFileFind)
    {
    lFileNames.push_back(sFolder); // this pushes the path of *.txt file inside list
    do
    {
    string tempBuff="";
    if (!strcmp(findData.cFileName , ".")) continue ; //skip .
    if (!strcmp(findData.cFileName , "..")) continue ; //skip ..
    if (findData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ){
    // if directory append and search recursive.
    string buff = "";
    buff = sFolder + "\\" + findData.cFileName ;

    list_folder(buff,lFileNames);
    }



    }while(::FindNextFileA(hFileFind, &findData));
    }
    FindClose(hFileFind);
    }


    int main()
    {

    /* finding *.txt from temp */
    string sFolder = "c:\\temp";
    list<string> lFileNames;

    list_folder(sFolder, lFileNames);

    list<string>::const_iterator iter;
    const list<string>::const_iterator end = lFileNames.end();
    for(iter = lFileNames.begin(); iter != end; ++iter)
    {
    cout << (*iter).c_str() << endl;
    }
    return 0;
    }

  8. #8
    Join Date
    Feb 2012
    Posts
    16

    Re: Strings in VC++ 6

    one more update.

    when I did
    findData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
    for the path string defaultTempPath = "c:\\Documents and Settings\\"+userName+"\\Local Settings\\Temporary Internet Files\\Content.IE5";

    all the directories inside the Content.IE5 folder is not recognized as folder by findData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY , it gives 0 (false) as output


  9. #9
    Join Date
    Feb 2012
    Posts
    16

    Re: Strings in VC++ 6

    I did this
    cout << "\n This is boolean value (" << findData.cFileName << ") : " << hex << (findData.dwFileAttributes);

    and I got output as 14 (hex)

    according to this http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx , there is nothing called 14

    whatis going wrong here ?

    is this platform specific? will this work on winxp/vista/win7 ?


    regards

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Strings in VC++ 6

    Quote Originally Posted by rajsha View Post
    according to this http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx , there is nothing called 14
    Of course there isn't. These attributes are bit flags, so what you have here is FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  11. #11
    Join Date
    Nov 2007
    Posts
    613

    Re: Strings in VC++ 6

    The condition
    Code:
    findData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
    is not good since dwFileAttributes holds more than just one flag. Mask the other flags with logical "and" to get only the flag you need:
    Code:
    (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY

  12. #12
    Join Date
    Feb 2012
    Posts
    16

    Re: Strings in VC++ 6

    Hi

    Im wring a code to search if my website has been downloaded by IE or not, this is done by searching a file recursive inside "c:\\Documents and Settings\\"+userName+"\\Local Settings\\Temporary Internet Files\\Content.IE5";

    I wanted to make it recursive search for a file name (abc.jpg) , i wrote this code (based on ovidiucucu's code), but its seems to be not working .
    Code:
    #include "stdafx.h"
    #pragma warning(disable: 4786)
    
    #include <iostream>
    #include <string>
    #include <list>
    
    using namespace std;
    
    void list_folder(string sFolderPtr, list<string>& lFileNames)
    {
       string& sFolder = sFolderPtr;
       string sFind = sFolder + "\\*.*"; 
       WIN32_FIND_DATA findData = {0};
       HANDLE hFileFind = ::FindFirstFileA(sFind.c_str(), &findData);   
       if(INVALID_HANDLE_VALUE != hFileFind)
       {	  
    	   
    	   do
          {		
    		string fileName = findData.cFileName;
    
    		if ( fileName.compare("accountmsg"))
    			cout << "Account msg found" ;
    		string tempBuff="";
    		if (!strcmp(findData.cFileName , ".")) continue ;
    		if (!strcmp(findData.cFileName , "..")) continue ;		
    		//cout << "\n This is boolean value (" << findData.cFileName << ") : " << hex << (findData.dwFileAttributes);// == FILE_ATTRIBUTE_COMPRESSED);
    		if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY){				
    			
    			string buff = "";
    			buff = sFolder + "\\" + findData.cFileName ;			
    			list_folder(buff,lFileNames);
    		} // if          
    
          }while(::FindNextFileA(hFileFind, &findData));
    	  //lFileNames.push_back((string)buff);
       }
       FindClose(hFileFind);
    }
    
    
    int main()
    {
    
    	string userName = (char*) getenv("username"); // temp Directory   
    	string defaultTempPath = "c:\\Documents and Settings\\"+userName+"\\Local Settings\\Temporary Internet Files\\Content.IE5";
    	string rootDir = "Content.IE5";  
     
    
    
    	   cout << "Starting \n";
    	   string sFolder = defaultTempPath;
    	   list<string> lFileNames;
    
    	   list_folder(sFolder, lFileNames);
    
    	   list<string>::const_iterator iter;
    	   const list<string>::const_iterator end = lFileNames.end();
    	   for(iter = lFileNames.begin(); iter != end; ++iter)
    	   {
    		 // cout << (*iter).c_str() << endl;
    	   }
    
    
      
    	return 0;
    }
    there is not error , but no desired output as well ....


    regards
    Last edited by ovidiucucu; February 29th, 2012 at 01:02 AM. Reason: added [CODE] tags

  13. #13
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: Strings in VC++ 6

    1. std::string::compare returns 0 if the strings are equal, so here is a possible mistake:
      Code:
      if ( fileName.compare("accountmsg"))
    2. don't trust in environment variables; they may differ or may be missing in diffrent systems.
    3. do not use hard-coded special folders like "c:\\Documents and Settings..."; they may differ in differnt languages and in different Windows versions.
      Instead, call SHGetSpecialFolderPath or SHGetFolderPath.
    4. last but not the least: in your code, no file name is added to the list; of course there is "no desired output as well"...


    All these being said, if you want to get a list of files having ".jpg" extension from "Content.IE5" subfolder of temporary Internet files folder, you have to write something like this:

    Code:
    #include "stdafx.h"
    // NOTE: <windows.h> and <shlobj.h> included in stdafx.h
    #pragma warning(disable: 4786)
    
    #include <iostream>
    #include <string>
    #include <list>
    
    DWORD list_folder(const std::string& sFolderRef,      // folder to search
                      std::list<std::string>& lFileNames, // file names list
                      const std::string& sExtRef)         // file extension
    {
       std::string sFind = sFolderRef + "\\*.*"; 
       WIN32_FIND_DATA findData = {0};
       HANDLE hFileFind = ::FindFirstFileA(sFind.c_str(), &findData);   
       if(INVALID_HANDLE_VALUE == hFileFind)
       {
           return ::GetLastError();
       }
       else
       {	  
          do
          {
             std::string sFileName = findData.cFileName;
             if(("." == sFileName) || (".." == sFileName)) continue;
             
             if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
             {
                list_folder(sFolderRef + "\\" + sFileName, lFileNames, sExtRef);
             }
             else
             {
                std::string sExt = sFileName.substr(sFileName.rfind(".") + 1);
                // compare extensions, non case-sensitive
                if(! stricmp(sExt.c_str(), sExtRef.c_str()))
                {
                   lFileNames.push_back(findData.cFileName);
                }
             }
             
          }while(::FindNextFileA(hFileFind, &findData));
          
       }
       ::FindClose(hFileFind);
       return NO_ERROR;
    }
    Code:
    int main()
    {
       // get internet temporary files special folder
       std::string sTempIntFiles;
       sTempIntFiles.resize(MAX_PATH);
       HRESULT hr = ::SHGetSpecialFolderPath(NULL, &sTempIntFiles[0], CSIDL_INTERNET_CACHE, FALSE);
       if(FAILED(hr))
       {
          std::cout << "SHGetSpecialFolderPath failed. Error: " << std::hex << hr << std::endl;
          return hr;
       }
       std::string sRootDir = sTempIntFiles.c_str();
       sRootDir += "\\Content.IE5";
    
       std::list<std::string> lFileNames;
    
       DWORD dwRet = list_folder(sRootDir, lFileNames, "jpg");
       if(NO_ERROR != dwRet)
       {
          std::cout << "list_folder failed. Error: " << std::dec << dwRet << std::endl;
          return dwRet;
       }
    
       std::list<std::string>::const_iterator iter;
       const std::list<std::string>::const_iterator end = lFileNames.end();
       for(iter = lFileNames.begin(); iter != end; ++iter)
       {
          std::cout << (*iter).c_str() << std::endl;
       }
       return 0;
    }


    [ later edit ]

    Please use CODE tags when posting source code examples.
    See: http://www.codeguru.com/forum/misc.php?do=bbcode#code
    Last edited by ovidiucucu; February 29th, 2012 at 04:04 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  14. #14
    Join Date
    Feb 2012
    Posts
    16

    Re: Strings in VC++ 6

    thanx a million

    I face this problem when compiled:-
    C:\project\web\test\Test_std_VS6.cpp(53) : error C2039: 'SHGetSpecialFolderPath' : is not a member of '`global namespace''
    C:\project\web\test\Test_std_VS6.cpp(53) : error C2065: 'SHGetSpecialFolderPath' : undeclared identifier
    C:\project\web\test\Test_std_VS6.cpp(53) : error C2065: 'CSIDL_INTERNET_CACHE' : undeclared identifier

  15. #15
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: Strings in VC++ 6

    Just take a look with a little bit more attention in my example and see this note:
    Quote Originally Posted by ovidiucucu View Post
    Code:
    #include "stdafx.h"
    // NOTE: <windows.h> and <shlobj.h> included in stdafx.h
    Also, you may have a look in SHGetSpecialFolderPath function documentation, focus on "Requirements" section and see which header file must be included.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)