Append to text file
#include <fstream>
std::ofstream ofstream1(MyFilepath.c_str(), std::ios_base::app); //Append to existing file if present
ofstream1 << "Hello";
ofstream1.close();
Overwrite A Text File
#include <fstream>
//****************************************
//****************************************
//********** CREATE A TEXT FILE **********
//****************************************
//****************************************
void create_some_text_file (void)
{
//----- CREATE A COPY OF THE ORIGINAL FILE IF THIS IS THE FIRST TIME WE'RE DOING THIS -----
struct stat sb;
int result = stat("/etc/wpa_supplicant/wpa_supplicant-original.conf", &sb);
if ((result != 0) || (sb.st_mode & S_IFDIR))
{
system("sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant-original.conf");
}
//----- CREATE NEW VERSION OF THE FILE -----
//You can use this if you want to use the contents of some existing file as part of the output:
// std::ifstream ifstream1("/etc/some_source_file.conf", std::ios_base::in);
//Then just use this wherever you want to insert it:
// output_file << ifstream1.rdbuf();
std::ofstream output_file("/etc/wpa_supplicant/wpa_supplicant.conf"); //Will overwrite an existing file
output_file << "# THIS FILE HAS BEEN AUTO GENERATED \n";
output_file << "\n";
output_file.close();
}
