C++: Where Does the Ofstream Class Save the Files To

C++: Where does the ofstream class save the files to?

The stream classes, like all other file-opening functions, use the current directory when you provide a relative path. You can control the current directory with a function like chdir, but a better solution is to use fully qualified file names. Then you remove your program's dependency on the current directory.

Where is my file stored if I write a file using ofstream on Mac?

I thought I would add an answer specific to Xcode. If you are building and running the executable in Xcode (the IDE), then the output file (if you did not specify an absolute path for the filename) will go to the same directory as the Build Products because that is where the built executable will be. This becomes the current working directory mentioned by Jesper Juhl when Xcode runs the executable. To locate that, click on the product in the Project Navigator (in the below screenshot this is the File Out executable in the left pane). Then look in the File Inspector in the upper right pane. The directory part of the Full Path is where your output file is.

Sample Image

If you did specify a relative path, then the location will be relative to this directory for build products, and as Jesper said, you should avoid encoding an absolute path in your program.

In Xcode, you can also change the current working directory by editing the scheme:

  1. Go to the Options tab under Run in the scheme editor.
  2. Click on the Use custom working directory checkbox
  3. Select or enter the working directory (absolute path)

Hope this helps.

Write a file in a specific path in C++

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.

C++ save pointer members with class when writing to file

Pointers refer to a specific location in memory. They can't be persisted to disk. When you start a new process and read them back, your process's memory layout will be different. So, what you're doing will not work.

You're going to have to write a custom serializer for your class, to handle its structure.

boost::serialization is a decent place to start: https://www.boost.org/doc/libs/1_75_0/libs/serialization/doc/tutorial.html

There's also cereal: https://uscilab.github.io/cereal/

As another commenter pointed out, this is a handy summary of the state of serialization in C++: https://isocpp.org/wiki/faq/serialization



Related Topics



Leave a reply



Submit