Guest User

Untitled

a guest
Jan 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ofstream file("sample.txt");
  10. file << "Hello, world!" << endl;
  11. file.close();
  12.  
  13. file.open("sample.txt", ios_base::ate);
  14. file << "Again hello, world!" << endl;
  15. file.close();
  16.  
  17. file.open("sample.txt", ios_base::ate);
  18. file << "And once again - hello, world!" << endl;
  19. file.close();
  20.  
  21. string str;
  22. ifstream ifile("sample.txt");
  23. while (getline(ifile, str))
  24. cout << str;
  25. }
  26.  
  27. // output: And once again - hello, world!
  28.  
  29. // Usage example: filePutContents("./yourfile.txt", "content", true);
  30. void filePutContents(const std::string& name, const std::string& content, bool append = false) {
  31. std::ofstream outfile;
  32. if (append)
  33. outfile.open(name, std::ios_base::app);
  34. else
  35. outfile.open(name);
  36. outfile << content;
  37. }
  38.  
  39. filePutContents("./yourfile.txt","content",true);
Add Comment
Please, Sign In to add comment