Sunday, May 06, 2018

C++ programming - using fstream in a cross-platform manner

Some notes about making use of fstream in cross-platform code - 

Some interesting methods are discussed at
https://stackoverflow.com/questions/9739948/write-a-file-in-a-specific-path-in-c/40980510#40980510

In Visual Studio,
disable deprecation, use _CRT_SECURE_NO_WARNINGS - 

Configuration Properties >> C/C++ >> Preporocessor >> Preprocessor Definitions >>  _CRT_SECURE_NO_WARNINGS

A working code snippet - 

    char path[80];
    char foldername[80];
    sprintf(path,"homeuser/file.txt");
    sprintf(foldername, "homeuser");
    CreateDirectoryA(foldername, NULL);
    std::ofstream file(path); //open in constructor
    std::string data("data to write to file");
    file << data;

No comments:

Post a Comment