Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int main()
- {
- ofstream file("sample.txt");
- file << "Hello, world!" << endl;
- file.close();
- file.open("sample.txt", ios_base::ate);
- file << "Again hello, world!" << endl;
- file.close();
- file.open("sample.txt", ios_base::ate);
- file << "And once again - hello, world!" << endl;
- file.close();
- string str;
- ifstream ifile("sample.txt");
- while (getline(ifile, str))
- cout << str;
- }
- // output: And once again - hello, world!
- // Usage example: filePutContents("./yourfile.txt", "content", true);
- void filePutContents(const std::string& name, const std::string& content, bool append = false) {
- std::ofstream outfile;
- if (append)
- outfile.open(name, std::ios_base::app);
- else
- outfile.open(name);
- outfile << content;
- }
- filePutContents("./yourfile.txt","content",true);
Add Comment
Please, Sign In to add comment