I successfully write to a file in the folder which run example:
// I run "test" executable file in "TestWrite File" folder
const char *path="/home/kingfisher/Desktop/TestWrite File/xml/kingfisher.txt";
std::ofstream file(path); //open in constructor
std::string data("data to write to file");
file << data;
However, If I try to write with dynamic path: *path = "/xml/kingfisher.txt", it goes wrong (in Windows, it will be fine)!! How I can write with dynamic path like above (not a specific path)? Thanks!
Not sure what you mean by “dynamic path”; a dynamic path is one that
will be read dynamically (and so will probably be in an
std::string).On the other hand, you seem to be confusing absolute path and relative
path. If the filename begins with a
'/'(under Unix), or with a'/'or a
'\\', possibly preceded by"d:"underWindows, it is absolute; the search for the file will start at the root
of the file system (on the specified drive in the case of Windows). In
all other cases, it is relative; the search for the file will start at
the current working directory. In your example, both
"/home/kingfisher/Desktop/TestWrite File/xml/kingfiger.txt"and"/xml/kingfisher.txt"are absolute. If the current working directoryis
"/home/kingfisher/Desktop/TestWrite File", then"xml/kingfisher.txt"should find the file specified by the firstabsolute pathname.