Search code examples
c++winapistlstdstring

Is there a way to get std:string's buffer


Is there a way to get the "raw" buffer o a std::string?
I'm thinking of something similar to CString::GetBuffer(). For example, with CString I would do:

CString myPath;  
::GetCurrentDirectory(MAX_PATH+1, myPath.GetBuffer(MAX_PATH));  
myPath.ReleaseBuffer();  

So, does std::string have something similar?


Solution

  • Use std::vector<char> if you want a real buffer.

    #include <vector>
    #include <string>
    
    int main(){
      std::vector<char> buff(MAX_PATH+1);
      ::GetCurrentDirectory(MAX_PATH+1, &buff[0]);
      std::string path(buff.begin(), buff.end());
    }
    

    Example on Ideone.