C++ fstream confusion

85 views Asked by At

I have a program that, multiple times throughout, uses fstream to save to files. It works perfectly when running in microsoft vs, but when I compile a release, and bring the .exe to my desktop, it won't write to any files there.

ofstream save;
save.open("Data\\options.scav");
/*Write stuff to file*/

This works fine in visual studio, but if I bring it to my desktop, and even create the Data folder for it, it doesn't write. Any help would be appreciated.

1

There are 1 answers

0
Angus Comber On BEST ANSWER

You cannot write to a path that has not already been created.

Do like this:

// check Data folder exists
ofstream save;
save.open("Data\\options.scav");

Note that for portability reasons you can also do this (on Windows or Unix):

// check Data folder exists
ofstream save;
save.open("Data/options.scav");