-
August 6th, 2009, 06:00 AM
#1
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!
-
August 6th, 2009, 06:33 AM
#2
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.
-
August 6th, 2009, 06:45 AM
#3
Re: Problem with system paths
Originally Posted by metzenes
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);
}
-
August 6th, 2009, 06:51 AM
#4
Re: Problem with system paths
Originally Posted by Marc G
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.
-
August 6th, 2009, 06:59 AM
#5
Re: Problem with system paths
Originally Posted by monarch_dodra
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...
-
August 6th, 2009, 07:07 AM
#6
Re: Problem with system paths
Originally Posted by metzenes
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
-
August 6th, 2009, 07:18 AM
#7
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!
-
August 6th, 2009, 07:31 AM
#8
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";
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|