#Save file for C++ code

6 messages · Page 1 of 1 (latest)

stone sparrow
#

well, I've made kind of silly program to practice ( I'm a beginner ) which is simply a to do list, the user inputs the tasks he wants to add and in the end the program prints them out , how can i possibly make these tasks get saved in file or whatever in the hard drive so i can see the tasks in the previous season? thank you in advance

uncut falcon
#
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
  ofstream myfile;
  myfile.open ("todos.txt");
  myfile << "This is a example todo that I want to save to a file.\n";
  myfile << "This is another todo that I want to save to a file.\n";
  myfile.close();
  return 0;
}

to save the data to the file

#

and something like that to read the data out of the file

#
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
  string line;
  ifstream myfile ("todos.txt");
  if (myfile.is_open())
  {
    // this will output every todo in the todos.txt
    while (getline(myfile,line))
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
#

i would suggest you to make a own function to read and write todos