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













0















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!!










share|improve this question
























  • 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












  • 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















0















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!!










share|improve this question
























  • 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












  • 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













0












0








0








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!!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • 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












  • 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











  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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.






    share|improve this answer





























      0














      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.






      share|improve this answer



























        0












        0








        0







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 6:49

























        answered Mar 8 at 6:43









        johnjohn

        38.4k12848




        38.4k12848





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme