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();
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *