Why isn't content written to a new text file?
<p>The program opens the file, creates a new.txt file, and opens it, but it doesn't fill the new.txt, and then proceeds to the renaming process.</p>
<p>Why doesn't it write to the new file? How should I properly redo this to make it write to the new file?</p>
<p>Here's my program:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstdlib&gt;
#include &lt;windows.h&gt;
#include &lt;stdexcept&gt;
#include &lt;limits&gt;
#include &lt;Lmcons.h&gt;
#include &lt;fstream&gt;
#include &lt;stdio.h&gt;


using namespace std;


void out(string x)
{
    x+=&quot;\n&quot;;
    cout&lt;&lt;x;
}
void outn()
{
    out(&quot;&quot;);
}
void delay(int x)
{
    Sleep(x);
}
void delayS(int x)
{
    Sleep(x*1000);
}
void cs()
{
    std::system(&quot;cls&quot;);
}
void UserName(string *x)
{
    char username[UNLEN + 1];
    DWORD size = UNLEN + 1;
    GetUserName(username, &amp;size);
    string transition(username);
    *x=transition;
}
//use this syntax in main : string username;UserName(&amp;username);



int main()
{


    string username;
    UserName(&amp;username);
    /*out(username);*/


    const string namefile=&quot;preferences.txt&quot;;


    string path;

    const string beginning=&quot;C:\\Users\\&quot;;
    const string ending=&quot;\\AppData\\Roaming\\Processing\\&quot;;

    path+=beginning;
    path+=username;
    path+=ending;

    string path2;
    path2=path;
    path2+=&quot;new.txt&quot;;
    string filename2=&quot;new.txt&quot;;

    path+=namefile;



    string line76=&quot;proxy.http.host=proxy-eple.in.ac-nantes.fr&quot;;
    string line77=&quot;3128&quot;;
    string line78=&quot;proxy.https.host=proxy-eple.in.ac-nantes.fr&quot;;
    string line79=&quot;3128&quot;;
    string line80=&quot;proxy.socks.host=proxy-eple.in.ac-nantes.fr&quot;;
    string line81=&quot;3128&quot;;


    fstream file;
    //fstream newfile;
    ofstream newfile(path2.c_str());

    try
    {
        file.open(path.c_str(),ios::in);
        if(!file.is_open())
        {
            throw 404;
        }
        //procédure de modif
        else
        {
            file.close();

            file.open(path.c_str());
            try
            {
                newfile.open(path2.c_str(),ios::out|ios::trunc);
                if(!newfile.is_open())
                {
                    throw 404;
                }
            }
            catch(int x)
            {
                cout&lt;&lt;&quot;Error &quot;&lt;&lt;x&lt;&lt;&quot; : failed to open file \\new.txt\\&quot;;
            }

            for (unsigned int i = 1; i &lt; 76; i++)
            {
                std::string text;
                getline(file, text);
                newfile &lt;&lt; text &lt;&lt;endl;
            }
            newfile &lt;&lt; line76 &lt;&lt;endl;
            newfile &lt;&lt; line77 &lt;&lt;endl;
            newfile &lt;&lt; line78 &lt;&lt;endl;
            newfile &lt;&lt; line79 &lt;&lt;endl;
            newfile &lt;&lt; line80 &lt;&lt;endl;
            newfile &lt;&lt; line81 &lt;&lt;endl;
            for (unsigned int i = 82; i &lt; 97; i++)
            {
                std::string text;
                std::getline(file, text);
                newfile &lt;&lt; text &lt;&lt;endl;
            }
            newfile &lt;&lt; &quot;&quot;;

        }

        file.close();
        newfile.close();
        string old1 = beginning+username+ending+&quot;old_pref.txt&quot;;

        try
        {
            int rf=rename(path.c_str(),old1.c_str());
            if(rf!=0)
            {
                throw 911;
            }
        }
        catch(int x)
        {
            cout&lt;&lt;&quot;Error &quot;&lt;&lt;x&lt;&lt;&quot; : Failed to rename to \\old_pref.txt\\&quot;;
        }
        try
        {
            int rf2=rename(path2.c_str(),path.c_str());
            if(rf2!=0)
            {
                throw 911;
            }
        }
        catch(int x)
        {
            cout&lt;&lt;&quot;Error &quot;&lt;&lt;x&lt;&lt;&quot; : Failed to rename to \\preferences.txt\\&quot;;
        }


        cs();
        out(&quot;done !&quot;);
    }
    catch(int x)
    {
        cout&lt;&lt;&quot;Error &quot;&lt;&lt;x&lt;&lt;&quot; : failed to open file \\preferences.txt\\&quot;;
    }


    return 0;
}
</code></pre>