Reading file path from user in simple format “c:filessample.txt”How do I create a Java string from the contents of a file?The Definitive C++ Book Guide and ListHow to get the filename without the extension from a path in Python?How to remove line breaks from a file in Java?Read whole ASCII file into C++ std::stringHow to read a file line-by-line into a list?Why is reading lines from stdin much slower in C++ than Python?Ukkonen's suffix tree algorithm in plain EnglishImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
Does the Mind Blank spell prevent the target from being frightened?
Is a model fitted to data or is data fitted to a model?
Drawing a topological "handle" with Tikz
Have I saved too much for retirement so far?
Engineer refusing to file/disclose patents
Can the Supreme Court overturn an impeachment?
Divine apple island
Longest common substring in linear time
Varistor? Purpose and principle
Transformation of random variables and joint distributions
What's the difference between 違法 and 不法?
What is the difference between "Do you interest" and "...interested in" something?
Diode in opposite direction?
How will losing mobility of one hand affect my career as a programmer?
Open a doc from terminal, but not by its name
Two-sided logarithm inequality
How must one send away the mother bird?
Query about absorption line spectra
How much character growth crosses the line into breaking the character
Bob has never been a M before
Will adding a BY-SA image to a blog post make the entire post BY-SA?
How do I implement a file system driver driver in Linux?
Is camera lens focus an exact point or a range?
What major Native American tribes were around Santa Fe during the late 1850s?
Reading file path from user in simple format “c:filessample.txt”
How do I create a Java string from the contents of a file?The Definitive C++ Book Guide and ListHow to get the filename without the extension from a path in Python?How to remove line breaks from a file in Java?Read whole ASCII file into C++ std::stringHow to read a file line-by-line into a list?Why is reading lines from stdin much slower in C++ than Python?Ukkonen's suffix tree algorithm in plain EnglishImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
I am trying to read file path from user as command line in simple format
ex:
path="c:filessample.txt"
but when trying to access using file.open("c:filessample.txt");
file not found.
so i used path.replace(path.begin(),path.end(),"\","\\")
for it to change to \
but its not working.
Help me!!
c++ string
add a comment |
I am trying to read file path from user as command line in simple format
ex:
path="c:filessample.txt"
but when trying to access using file.open("c:filessample.txt");
file not found.
so i used path.replace(path.begin(),path.end(),"\","\\")
for it to change to \
but its not working.
Help me!!
c++ string
A single '' in a string is an escape character, but not a "" , the real string of the path is"c:(f)iles(s)ample.txt"
, so the replace function will not find the "".
– Drake Wu - MSFT
Mar 8 at 6:07
Instead ofpath.replace
, why not initializepath
asC:\files\sample.txt
?
– Sailesh D
Mar 8 at 6:08
end user is not aware about computer field they can just copy the file path and paste so it could create problems further.
– lalit Kohli
Mar 8 at 6:18
You don't need to double up on backslashes in runtime data, only in compile-time literals.
– Remy Lebeau
Mar 8 at 6:20
To be not confused with char escaping use the special form of defining string liberals that disables char escaping:file.open(R"(c:filessample.txt)");
– S.M.
Mar 8 at 6:37
add a comment |
I am trying to read file path from user as command line in simple format
ex:
path="c:filessample.txt"
but when trying to access using file.open("c:filessample.txt");
file not found.
so i used path.replace(path.begin(),path.end(),"\","\\")
for it to change to \
but its not working.
Help me!!
c++ string
I am trying to read file path from user as command line in simple format
ex:
path="c:filessample.txt"
but when trying to access using file.open("c:filessample.txt");
file not found.
so i used path.replace(path.begin(),path.end(),"\","\\")
for it to change to \
but its not working.
Help me!!
c++ string
c++ string
edited Mar 8 at 5:48
Ansi
19138
19138
asked Mar 8 at 5:42
lalit Kohlilalit Kohli
11
11
A single '' in a string is an escape character, but not a "" , the real string of the path is"c:(f)iles(s)ample.txt"
, so the replace function will not find the "".
– Drake Wu - MSFT
Mar 8 at 6:07
Instead ofpath.replace
, why not initializepath
asC:\files\sample.txt
?
– Sailesh D
Mar 8 at 6:08
end user is not aware about computer field they can just copy the file path and paste so it could create problems further.
– lalit Kohli
Mar 8 at 6:18
You don't need to double up on backslashes in runtime data, only in compile-time literals.
– Remy Lebeau
Mar 8 at 6:20
To be not confused with char escaping use the special form of defining string liberals that disables char escaping:file.open(R"(c:filessample.txt)");
– S.M.
Mar 8 at 6:37
add a comment |
A single '' in a string is an escape character, but not a "" , the real string of the path is"c:(f)iles(s)ample.txt"
, so the replace function will not find the "".
– Drake Wu - MSFT
Mar 8 at 6:07
Instead ofpath.replace
, why not initializepath
asC:\files\sample.txt
?
– Sailesh D
Mar 8 at 6:08
end user is not aware about computer field they can just copy the file path and paste so it could create problems further.
– lalit Kohli
Mar 8 at 6:18
You don't need to double up on backslashes in runtime data, only in compile-time literals.
– Remy Lebeau
Mar 8 at 6:20
To be not confused with char escaping use the special form of defining string liberals that disables char escaping:file.open(R"(c:filessample.txt)");
– S.M.
Mar 8 at 6:37
A single '' in a string is an escape character, but not a "" , the real string of the path is
"c:(f)iles(s)ample.txt"
, so the replace function will not find the "".– Drake Wu - MSFT
Mar 8 at 6:07
A single '' in a string is an escape character, but not a "" , the real string of the path is
"c:(f)iles(s)ample.txt"
, so the replace function will not find the "".– Drake Wu - MSFT
Mar 8 at 6:07
Instead of
path.replace
, why not initialize path
as C:\files\sample.txt
?– Sailesh D
Mar 8 at 6:08
Instead of
path.replace
, why not initialize path
as C:\files\sample.txt
?– Sailesh D
Mar 8 at 6:08
end user is not aware about computer field they can just copy the file path and paste so it could create problems further.
– lalit Kohli
Mar 8 at 6:18
end user is not aware about computer field they can just copy the file path and paste so it could create problems further.
– lalit Kohli
Mar 8 at 6:18
You don't need to double up on backslashes in runtime data, only in compile-time literals.
– Remy Lebeau
Mar 8 at 6:20
You don't need to double up on backslashes in runtime data, only in compile-time literals.
– Remy Lebeau
Mar 8 at 6:20
To be not confused with char escaping use the special form of defining string liberals that disables char escaping:
file.open(R"(c:filessample.txt)");
– S.M.
Mar 8 at 6:37
To be not confused with char escaping use the special form of defining string liberals that disables char escaping:
file.open(R"(c:filessample.txt)");
– S.M.
Mar 8 at 6:37
add a comment |
1 Answer
1
active
oldest
votes
As you seem to know
path="c:filessample.txt";
is incorrect, it should be
path="c:\files\sample.txt";
(or forward slashes work as well)
path="c:/files/sample.txt";
But this is also incorrect
path="c:filessample.txt";
path.replace(path.begin(),path.end(),"\","\\");
The second line replaces all the single backslashes with double backslashes, but there are no single backslashes in your original string.
f
is an escape sequence for the form feed character, and s
not even a legal character sequence.
Escape sequences are used in string literals to denote characters which would otherwise be hard to write. For instance you cannot easily put "
in a string literal because it would end the string, so the escape sequence "
exists to let you do this. Similarly since the backslash character is used to start an escape sequence, the escape sequence \
stands for the backslash character itself.
These rules apply to string literals only, if you are reading a string from the user then there is no need to replace the backslashes with double backslashes, that makes no sense because single backslashes are what you want in a file path. It's just that the way to get single backslashes in a string literal is to write double backslashes.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057345%2freading-file-path-from-user-in-simple-format-c-files-sample-txt%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As you seem to know
path="c:filessample.txt";
is incorrect, it should be
path="c:\files\sample.txt";
(or forward slashes work as well)
path="c:/files/sample.txt";
But this is also incorrect
path="c:filessample.txt";
path.replace(path.begin(),path.end(),"\","\\");
The second line replaces all the single backslashes with double backslashes, but there are no single backslashes in your original string.
f
is an escape sequence for the form feed character, and s
not even a legal character sequence.
Escape sequences are used in string literals to denote characters which would otherwise be hard to write. For instance you cannot easily put "
in a string literal because it would end the string, so the escape sequence "
exists to let you do this. Similarly since the backslash character is used to start an escape sequence, the escape sequence \
stands for the backslash character itself.
These rules apply to string literals only, if you are reading a string from the user then there is no need to replace the backslashes with double backslashes, that makes no sense because single backslashes are what you want in a file path. It's just that the way to get single backslashes in a string literal is to write double backslashes.
add a comment |
As you seem to know
path="c:filessample.txt";
is incorrect, it should be
path="c:\files\sample.txt";
(or forward slashes work as well)
path="c:/files/sample.txt";
But this is also incorrect
path="c:filessample.txt";
path.replace(path.begin(),path.end(),"\","\\");
The second line replaces all the single backslashes with double backslashes, but there are no single backslashes in your original string.
f
is an escape sequence for the form feed character, and s
not even a legal character sequence.
Escape sequences are used in string literals to denote characters which would otherwise be hard to write. For instance you cannot easily put "
in a string literal because it would end the string, so the escape sequence "
exists to let you do this. Similarly since the backslash character is used to start an escape sequence, the escape sequence \
stands for the backslash character itself.
These rules apply to string literals only, if you are reading a string from the user then there is no need to replace the backslashes with double backslashes, that makes no sense because single backslashes are what you want in a file path. It's just that the way to get single backslashes in a string literal is to write double backslashes.
add a comment |
As you seem to know
path="c:filessample.txt";
is incorrect, it should be
path="c:\files\sample.txt";
(or forward slashes work as well)
path="c:/files/sample.txt";
But this is also incorrect
path="c:filessample.txt";
path.replace(path.begin(),path.end(),"\","\\");
The second line replaces all the single backslashes with double backslashes, but there are no single backslashes in your original string.
f
is an escape sequence for the form feed character, and s
not even a legal character sequence.
Escape sequences are used in string literals to denote characters which would otherwise be hard to write. For instance you cannot easily put "
in a string literal because it would end the string, so the escape sequence "
exists to let you do this. Similarly since the backslash character is used to start an escape sequence, the escape sequence \
stands for the backslash character itself.
These rules apply to string literals only, if you are reading a string from the user then there is no need to replace the backslashes with double backslashes, that makes no sense because single backslashes are what you want in a file path. It's just that the way to get single backslashes in a string literal is to write double backslashes.
As you seem to know
path="c:filessample.txt";
is incorrect, it should be
path="c:\files\sample.txt";
(or forward slashes work as well)
path="c:/files/sample.txt";
But this is also incorrect
path="c:filessample.txt";
path.replace(path.begin(),path.end(),"\","\\");
The second line replaces all the single backslashes with double backslashes, but there are no single backslashes in your original string.
f
is an escape sequence for the form feed character, and s
not even a legal character sequence.
Escape sequences are used in string literals to denote characters which would otherwise be hard to write. For instance you cannot easily put "
in a string literal because it would end the string, so the escape sequence "
exists to let you do this. Similarly since the backslash character is used to start an escape sequence, the escape sequence \
stands for the backslash character itself.
These rules apply to string literals only, if you are reading a string from the user then there is no need to replace the backslashes with double backslashes, that makes no sense because single backslashes are what you want in a file path. It's just that the way to get single backslashes in a string literal is to write double backslashes.
edited Mar 8 at 6:49
answered Mar 8 at 6:43
johnjohn
38.4k12848
38.4k12848
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057345%2freading-file-path-from-user-in-simple-format-c-files-sample-txt%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
A single '' in a string is an escape character, but not a "" , the real string of the path is
"c:(f)iles(s)ample.txt"
, so the replace function will not find the "".– Drake Wu - MSFT
Mar 8 at 6:07
Instead of
path.replace
, why not initializepath
asC:\files\sample.txt
?– Sailesh D
Mar 8 at 6:08
end user is not aware about computer field they can just copy the file path and paste so it could create problems further.
– lalit Kohli
Mar 8 at 6:18
You don't need to double up on backslashes in runtime data, only in compile-time literals.
– Remy Lebeau
Mar 8 at 6:20
To be not confused with char escaping use the special form of defining string liberals that disables char escaping:
file.open(R"(c:filessample.txt)");
– S.M.
Mar 8 at 6:37