Posted By

Desoxena on 01/11/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Sereniel


writes to a log


 / Published in: C++
 

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. void logWriteTo()
  10. {
  11. ofstream myLog;
  12. string command;
  13. string logWrite;
  14. myLog.open("log.txt", ios::app);
  15. cout << "new entry?" << endl;
  16. cin >> command;
  17. if (command == "yes")
  18. {
  19. system("cls");
  20. cin >> logWrite;
  21. myLog << ">>> " << logWrite << "\n\n";
  22. }
  23. myLog.close();
  24. ;
  25. }
  26. void logView()
  27. {
  28. cout << "your recent entries:\n\n\n" << endl;
  29. system("cls");
  30. string line;
  31. ifstream myLog ("log.txt");
  32. if (myLog.is_open())
  33. {
  34. while ( myLog.good() )
  35. {
  36. getline (myLog, line);
  37. cout << line << endl;
  38. }
  39. myLog.close();
  40. system("pause");
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. string command;
  47. cout << "would you like to use the logwriter?" << endl;
  48. cin >> command;
  49. if (command == "yes")
  50. {
  51. logWriteTo();
  52. logView();
  53. }
  54. }
  55. //input: ">>> hello.how.has.your.day.been?" (sadly, I have no idea how to input white spaces //with cin yet.)

Report this snippet  

You need to login to post a comment.