create file on desktop in c++ -


I know that we use the following code to create a file in c ++

  #include & lt; Iostream & gt; # Include & lt; Fstream & gt; using namespace std; Int main () {offstream out; Out.open ("exemple.txt"); Return 0; }  

I have to follow my question. I want to create a desktop.txt file on the desktop or in another directory, how to make the desktop easier to do? Please help with this kind of help?

  out.open ("example.txt", "C: \ Users \ David \ Desktop"); The main problem with your code is that '\' has the escape character C / \ C ++  

So when you put the string: "C: \ users \ David \ Desktop" slash is running from the next character and thus they are not really part of the string and Do

  • Use escape sequence for slash '\\' "C: UsersDavidDesktop" to compensate for it There are two options for:

    • / code> like this: "C: \\ user \\ David \\ Desktop"
    • Or use to isolate the character from '/' Do directories in the path
      • Personally I like this option because it is portable among all modern OS. (Win / Linux / Mac)

    Your secondary problem is that you are using it in an open way, just type the path name as 1 long string Specify in (this is called a full path). Personally I prefer giving the constructor a file name instead of calling the open phone clearly (but this is just a personal preference thing).

      #include & lt; Fstream & gt; Int main () {std :: ofstream out1 ("C: \\ user \\ David \\ Desktop \ exemple1.txt"); Std :: ofstream out2 ("C: / Users / David / Desktop / XMPlay 2.txt"); }  

    A nominal note. Hard coding for the desktop directory is not good practice. You are coupling tightly with your application that how the file system is shown to that version of OS. For more information on how to obtain the directories of each OS usually users of the user, see a technique on your OS document (or ask another question on stack overflow).

    In addition to this, it is useful to know how it works, instead of representing files in the form of stars, Saran is the main OS in the file system class to promote the note; When you create complex paths (for example paths with spaces), string representation may get a little error prone. Inserted characters inside them


Comments

Popular posts from this blog

paypal - How to know the URL referrer in PHP? -

oauth - Facebook OAuth2 Logout does not remove fb_ cookie -

wpf - Line breaks and indenting for the XAML of a saved FlowDocument? -