Write a file in a specific path in C++
Asked Answered
D

7

19

I have this code that writes successfully a file:

    ofstream outfile (path);
    outfile.write(buffer,size);
    outfile.flush();
    outfile.close();

buffer and size are ok in the rest of code. How is possible put the file in a specific path?

Dioxide answered 16/3, 2012 at 15:20 Comment(4)
Check the documentation of your file system and your platform.Oatcake
How did you construct outfile?Ronironica
there is no file opening in your code. It is what determines the nameMalony
What platform and framework are you using?Oralee
B
17

Specify the full path in the constructor of the stream, this can be an absolute path or a relative path. (relative to where the program is run from)

The streams destructor closes the file for you at the end of the function where the object was created(since ofstream is a class).

Explicit closes are a good practice when you want to reuse the same file descriptor for another file. If this is not needed, you can let the destructor do it's job.

#include <fstream>
#include <string>

int main()
{
    const char *path="/home/user/file.txt";
    std::ofstream file(path); //open in constructor
    std::string data("data to write to file");
    file << data;
}//file destructor

Note you can use std::string in the file constructor in C++11 and is preferred to a const char* in most cases.

Beatriz answered 16/3, 2012 at 15:45 Comment(2)
In C++ versions prior to C++11, use std::string::c_str() to get the C-Language string type required by the constructor or open methods.Barrington
@ThomasMatthews this is true and I would recommend to still use a string in the way you recommend, however I was trying to show that ofstream takes a const char* (which can obtained in the way that you show.Beatriz
H
10

Rationale for posting another answer

I'm posting because none of the other answers cover the problem space.

The answer to your question depends on how you get the path. If you are building the path entirely within your application then see the answer from @James Kanze. However, if you are reading the path or components of the path from the environment in which your program is running (e.g. environment variable, command-line, config files etc..) then the solution is different. In order to understand why, we need to define what a path is.

Quick overview of paths

On the operating systems (that I am aware of), a path is a string which conforms to a mini-language specified by the operating-system and file-system (system for short). Paths can be supplied to IO functions on a given system in order to access some resource. For example here are some paths that you might encounter on Windows:

\file.txt
\\bob\admin$\file.txt
C:..\file.txt
\\?\C:\file.txt
.././file.txt
\\.\PhysicalDisk1\bob.txt
\\;WebDavRedirector\bob.com\xyz
C:\PROGRA~1\bob.txt
.\A:B

Solving the problem via path manipulation

Imagine the following scenario: your program supports a command line argument, --output-path=<path>, which allows users to supply a path into which your program should create output files. A solution for creating files in the specified directory would be:

  1. Parse the user specified path based on the mini-language for the system you are operating in.
  2. Build a new path in the mini-language which specifies the correct location to write the file using the filename and the information you parsed in step 1.
  3. Open the file using the path generated in step 2.

An example of doing this:

On Linux, say the user has specified --output-path=/dir1/dir2

Parse this mini-language:

/dir1/dir2
--> "/" root
--> "dir1" directory under root
--> "/" path seperator
--> "dir2" directory under dir1

Then when we want to output a file in the specified directory we build a new path. For example, if we want to output a file called bob.txt, we can build the following path:

/dir1/dir2/bob.txt
--> "/" root
--> "dir1" directory under root
--> "/" path separator
--> "dir2" directory under dir1
--> "/" path seperator
--> "bob.txt" file in directory dir2

We can then use this new path to create the file.

In general it is impossible to implement this solution fully. Even if you could write code that could successfully decode all path mini-languages in existence and correctly represent the information about each system so that a new path could be built correctly - in the future your program may be built or run on new systems which have new path mini-languages that your program cannot handle. Therefore, we need to use a careful strategy for managing paths.

Path handling strategies

1. Avoid path manipulation entirely

Do not attempt to manipulate paths that are input to your program. You should pass these strings directly to api functions that can handle them correctly. This means that you need to use OS specific api's directly avoiding the C++ file IO abstractions (or you need to be absolutely sure how these abstractions are implemented on each OS). Make sure to design the interface to your program carefully to avoid a situation where you might be forced into manipulating paths. Try to implement the algorithms for your program to similarly avoid the need to manipulate paths. Document the api functions that your program uses on each OS to the user - this is because OS api functions themselves become deprecated over time so in future your program might not be compatible with all possible paths even if you are careful to avoid path manipulation.

2. Document the functions your program uses to manipulate paths

Document to the user exactly how paths will be manipulated. Then make it clear that it is the users responsibility to specify paths that will work correctly with the documented program behavior.

3. Only support a restricted set of paths

Restrict the path mini-languages your program will accept until you are confident that you can correctly manipulate the subset of paths that meet this set of restrictions. Document this to the user. Error if paths are input that do not conform.

4. Ignore the issues

Do some basic path manipulation without worrying too much. Accept that your program will exhibit undefined behavior for some paths that are input. You could document to the user that the program may or may not work when they input paths to it, and that it is the users responsibly to ensure that the program has handled the input paths correctly. However, you could also not document anything. Users will commonly expect that your program will not handle some paths correctly (many don't) and therefore will cope well even without documentation.

Closing thoughts

It is important to decide on an effective strategy for working with paths early on in the life-cycle of your program. If you have to change how paths are handled later it may be difficult to avoid a change in behaviour that might break the your program for existing users.

Heflin answered 5/12, 2016 at 18:4 Comment(0)
G
1

Try this:

ofstream outfile;
string createFile = "";    
string path="/FULL_PATH";

createFile = path.as<string>() + "/" + "SAMPLE_FILENAME" + ".txt";          
outfile.open(createFile.c_str());       
outfile.close();

//It works like a charm.
Girard answered 29/12, 2016 at 3:52 Comment(1)
What's the path.as<string>() function? I've never seen anything like that before.Arlettaarlette
C
0

That needs to be done when you open the file, see std::ofstream constructor or open() member.

Cabinet answered 16/3, 2012 at 15:24 Comment(0)
H
0

It's not too clear what you're asking; if I understand correctly, you're given a filename, and you want to create the file in a specific directory. If that's the case, all that's necessary is to specify the complet path to the constructor of ofstream. You can use string concatenation to build up this path, but I'd strongly recommend boost::filesystem::path. It has all of the functions to do this portably, and a lot more; otherwise, you'll not be portable (without a lot of effort), and even simple operations on the filename will require considerable thought.

Huppert answered 16/3, 2012 at 15:45 Comment(2)
Does the ofstream constructor or open() method create missing directories in the path?Ronironica
Implementation defined, but I don't know of an implementation which does. Another reason to use boost::filesystem: it has a simple function that will do that if needed.Huppert
L
0

I was stuck on this for a while and have since figured it out. The path is based off where your executable is and varies a little. For this example assume you do a ls while in your executable directory and see:

myprogram.out  Saves  

Where Saves is a folder and myprogram.out is the program you are running.

In your code, if you are converting chars to a c_str() in a manner like this:

string file;
getline(cin, file, '\n');
ifstream thefile;
thefile.open( ("Saves/" + file + ".txt").c_str() );

and the user types in savefile, it would be

"Saves/savefile.txt"   

which will work to get to to get to savefile.txt in your Saves folder. Notice there is no pre-slashes and you just start with the folder name.

However if you are using a string literal like

ifstream thefile;
thefile.open("./Saves/savefile.txt");

it would be like this to get to the same folder:

"./Saves/savefile.txt"  

Notice you start with a ./ in front of the foldername.

Lands answered 14/11, 2016 at 20:50 Comment(0)
M
-1

If you are using linux, try execl(), with the command mv.

Mantinea answered 16/3, 2012 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.