CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2009
    Posts
    13

    Problem with system paths

    Hi!

    I'm working with strings containing file paths with the format:
    "C:\Folder1\Folder2\fileName.txt"

    The problem is that some methods I'm going to use later (mkdir, etc.) don't like this format and they use:
    "C:\\Folder1\\Folder2\\fileName.txt"

    So the question is, how can I change character '\' for string "\\" in a string?

    I have taken a look to string class, also I've tried with:

    replace(str_in.begin(), str_in.end(), "\\", "\\\\") from library <algorithm>

    with no success, since replace requires a character, not a string.


    Thanks in advance!

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Problem with system paths

    There is no need for this.
    When you write paths in your source code you need to use double slashes because the compiler will otherwise interprete them as escape charaters.
    However, if you are just creating strings at runtime, there is no need to have the double slashes in your string buffer.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem with system paths

    Quote Originally Posted by metzenes View Post
    Hi!
    replace(str_in.begin(), str_in.end(), "\\", "\\\\") from library <algorithm>
    You'll have no real chance to succeed using stl algorithms, as you need to transform 1 character into 2 characters.
    You could write your own function to use with an algorithm, but I feel a simple for loop could be better.

    Code:
    std::string addSlash(const std::string& iString)
    {
        std::string::const_iterator it(iString.begin());
        const std::string::const_iterator itEnd(iString.end());
        
        std::string aString;
        aString.reserve(iString.size()+std::count(it,itEnd,'\\'));
        
        for( ; it!=itEnd; ++it)
        {
            aString.push_back(*it);
            if (*it == '\\') aString.push_back(*it); //pushback again
        }
        return aString;
    }
    
    int main(int argc, char *argv[])
    {
        std::string a("hello \\ yo"); //There is only 1 backslash in this string, actually.
    
        std::cout << a.size() << std::endl;
        std::cout << a << std::endl;
        std::cout << addSlash(a) << std::endl;
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Notice this will not actually change your input string. If you want to modify it, I wouldn't change the algorithm, I'd just swap at the end. This is more efficient than inserting in the middle:
    Code:
    void addSlash(std::string& iString)
    {
        std::string::const_iterator it(iString.begin());
        const std::string::const_iterator itEnd(iString.end());
        
        std::string aString;
        aString.reserve(iString.size()+std::count(it,itEnd,'\\'));
        
        for( ; it!=itEnd; ++it)
        {
            aString.push_back(*it);
            if (*it == '\\') aString.push_back(*it); //pushback again
        }
        iString.swap(aString);
    }

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem with system paths

    Quote Originally Posted by Marc G View Post
    There is no need for this.
    When you write paths in your source code you need to use double slashes because the compiler will otherwise interprete them as escape charaters.
    However, if you are just creating strings at runtime, there is no need to have the double slashes in your string buffer.
    Yes, what Marc said. You must understand that \ is an escape character. Writing '\\' in c++ is actually a single character, and the string "c:\\programs" actually contains only 1 backslash. Most probably you shouldn't need my solution.

  5. #5
    Join Date
    Jul 2009
    Posts
    13

    Re: Problem with system paths

    Quote Originally Posted by monarch_dodra View Post
    Yes, what Marc said. You must understand that \ is an escape character. Writing '\\' in c++ is actually a single character, and the string "c:\\programs" actually contains only 1 backslash. Most probably you shouldn't need my solution.
    Yes, but the thing it's that I'm reading a file path from a text file, with the format

    C:\Folder\fileName.txt

    Some methods don't read '\' as a backslash, but an escape character combined with 'f' ('\f'). So that's why I need to change a single backslash for a double backslash.

    I'm going to try your suggestion! Let's see...

  6. #6
    Join Date
    Jul 2009
    Posts
    13

    Re: Problem with system paths

    Quote Originally Posted by metzenes View Post
    Yes, but the thing it's that I'm reading a file path from a text file, with the format

    C:\Folder\fileName.txt

    Some methods don't read '\' as a backslash, but an escape character combined with 'f' ('\f'). So that's why I need to change a single backslash for a double backslash.

    I'm going to try your suggestion! Let's see...
    Here an example of what I try to do:

    Code:
    #include<iostream>
    #include<string>
    #include <algorithm>
    
    using namespace std;
    
    int main(){
    
      string file = "L:\Source\cpp\Data\a.txt";
    
      replace(file.begin(), file.end(), '\\', '/');
    
      size_t found;
      found = file.find_last_of("/");
    
      string fileFolder = file.substr(0,found);
      string fileName = file.substr(found+1);
    
      cout << file << endl;
      cout << fileFolder << endl;
      cout << fileName << endl;
    
      return 0;
    }
    I get this warnings:
    warning: unknown escape sequence '\S'|
    warning: unknown escape sequence '\c'|
    warning: unknown escape sequence '\D'|

    and of course it doesn't work properly

  7. #7
    Join Date
    Jul 2009
    Posts
    13

    Re: Problem with system paths

    You were right guys (of course!).

    While I get errors working with a string defined by me ("L:\Source\file.txt"), it works nice if I read the path from a text file.

    Thanks!

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Problem with system paths

    Exactly. If you read it from file, single slashes are fine. In code however, this:
    Code:
    string file = "L:\Source\cpp\Data\a.txt";
    should be written as:
    Code:
    string file = "L:\\Source\\cpp\\Data\\a.txt";
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured