How to read and write to a text file in C++?
<p>Hey everyone, I have just started to learn C++ and I wanted to know how to read and write to a text file. I have seen many examples but they have all been hard to understand/follow and they have all varied. I was hoping that someone here could help. I am a total beginner so I need clear instructions. Here is an example of what i'm trying to do: </p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;
string usreq, usr, yn, usrenter;
int start ()
{
    cout &lt;&lt; "Welcome..."
int main ()
{
    cout &lt;&lt; "Is this your first time using TEST" &lt;&lt; endl;
    cin &gt;&gt; yn;
    if (yn == "y")
        {
            ofstream iusrfile;
            ofstream ousrfile;
            iusrfile.open("usrfile.txt", "w");
            iusrfile &gt;&gt; usr;
            cout &lt;&lt; iusrfile;
            iusrfile.close();
            cout &lt;&lt; "Please type your Username. \n";
            cin &gt;&gt; usrenter;
            if (usrenter == usr)
            {
            start ();
            }
        }
    else
        {
            cout &lt;&lt; "THAT IS NOT A REGISTERED USERNAME.";
        }

    return 0;

}
</code></pre>
