tags:

views:

97

answers:

4

i know that to create file in c++ we use following code

#include <iostream>
#include <fstream>
using namespace std;
int main(){

ofstream out;
out.open("exemple.txt");



 return 0;
}

my question is following i want create example.txt file on desktop or in other directory for simplify let's take desktop how? please help like this?

out.open("example.txt","C:\Users\David\Desktop");
+1  A: 

Use SHGetSpecialFolderPath function to get desktop folder path in current user http://msdn.microsoft.com/en-us/library/bb762204%28VS.85%29.aspx

Northern
+3  A: 

Here's a straight forward way to do it on Linux.

string path(getenv("HOME"));
path += "/Desktop/example.txt";
ofstream outf(path);
Matt Joiner
+5  A: 

The main problem with your code is that the '\' is the escape character in C/C++.

So when you put the string: "C:\Users\David\Desktop" The slashes are escaping the next character and thus they are not actually part of the string and what the executable gets is "C:UsersDavidDesktop" to compensate for this there are two alternatives:

  • Use the escape sequence for the slash '\\' thus giving you: "C:\\Users\\David\\Desktop"
  • Or use the '/' character to separate directories in the path.
    • Personally I prefer this option as it is portable between all modern OS's now. (Win/Linux/MAC)

Your secondary problem is that you are using the open incorrectly. Just specify the path name as 1 long string (this is called an absolute path). Personally I prefer to provide the file name to the constructor rather than explicitly calling open (but that's just a personal preference thing).

#include <fstream>
int main()
{
    std::ofstream out1("C:\\Users\\David\\Desktop\\exemple1.txt");

    std::ofstream out2("C:/Users/David/Desktop/exemple2.txt");
}

A minor note. Hard coding the path to the desktop directory is not good practice. You are tightly coupling your application to how that version of the OS lays out the file system. Each OS usually provides a technique on how to find users directories please see your OS documentation for more details (or ask another question on StackOverflow).

Also note boost provides a file system class to abstract the file system across all major OS's. Its useful to get to know how it works rather than representing files as strings; the string representation can get slightly error prone when you build complex paths (eg paths with spaces). characters embedded into them.

Martin York
"Personally I prefer this option as it is portable between all modern OS's now. (Win/Linux/MAC)" I was unaware of this and I am really very glad I found that out. That will come in handy, thanks!
Jeremy Kemball
@Jeremy Kemball: If you use the boost file sytem classes this is abstracted away. So please check it out. There are lots of OS/file system specific features when it comes to path names that is definitely worth using the boost system as you will be insulated from all the differences.
Martin York
I have heard nothing but good things about boost, so I might as well.
Jeremy Kemball
A: 
    char szPath[MAX_PATH] = {0};
    SHGetSpecialFolderPath(NULL,
                       szpath,
                       CSIDL_DESKTOPDIRECTORY,
                       FALSE);
    strcat(szPath,"\\Test.txt");

    ofstream out;
    out.open(szPath);
Trinity