如何在C ++中将新数据追加到文本文件中而不再次输出标题?

将代码输出到文本文件时出现问题。我只是想在.txt文件的开头打印标题,但最终却随新数据行一起打印。基本上,我使用ios :: app将新数据附加到文本文件中。附加其他数据后,如何实现不再次在文本文件中打印标题?

Text File Output

这是我的代码顺便说一句...

void QuaranDiary()
{
    int i = 0, update = 0;
    char QuaranDiaryMore;

    do
    {
        cout << "=====================================================================================" << endl;
        cout << "                                 COVID-19 QUARANDIARY                                " << endl;
        cout << "=====================================================================================" << endl << endl;
        cout << "DATE (MM/DD/YYYY)" << endl << endl;
        cout << "Month  : ";
        cin >> diary[i].QuaranDiaryMonth;
        cout << "Day    : ";
        cin >> diary[i].QuaranDiaryDay;
        cout << "Year   : ";
        cin >> diary[i].QuaranDiaryYear;
        cout << endl;
        cout << "[" << diary[i].QuaranDiaryMonth << "/" << diary[i].QuaranDiaryDay << "/" << diary[i].QuaranDiaryYear << "]" << endl << endl;

        cout << "TIME (23:59)"<<endl<<endl;
        cout << "Hour   : ";
        cin >> diary[i].QuaranDiaryHour;
        cout << "Minute : ";
        cin >> diary[i].QuaranDiaryMinute;
        cout << endl;
        cout << "[" << diary[i].QuaranDiaryHour << ":" << diary[i].QuaranDiaryMinute << "]" << endl << endl;

        cout << "Health Update: ";
        cin.ignore();
        getline(cin, diary[i].QuaranDiaryUpdate);

        update++;

        cout << "\n\nAdd More to QuaranDiary? [Y/N]: ";
        cin >> QuaranDiaryMore;
        QuaranDiaryMore = toupper(QuaranDiaryMore);

        while (toupper(QuaranDiaryMore) != 'Y' && toupper(QuaranDiaryMore) != 'N')
        {
            cout << "\nWrong Input. Try Again!\n" << endl;
            cin.clear();
            cin.ignore();
            cout << "Add More to QuaranDiary? [Y/N]: ";
            cin >> QuaranDiaryMore;
        }

        cin.clear();
        cin.ignore();
        system("CLS");

    } while (toupper(QuaranDiaryMore == 'Y'));

    ofstream QuaranDiary;
    QuaranDiary.open("QuaranDiary.txt", ios::app);

    QuaranDiary << "=====================================================================================" << endl;
    QuaranDiary << "                                 COVID-19 QUARANDIARY                                " << endl;
    QuaranDiary << "=====================================================================================" << endl << endl;

    for (i = 0; i < update; i++)
    {
        QuaranDiary << "Date: " << diary[i].QuaranDiaryMonth << "/" << diary[i].QuaranDiaryDay << "/" << diary[i].QuaranDiaryYear;
        QuaranDiary << "\tTime: " << diary[i].QuaranDiaryHour << ":" << diary[i].QuaranDiaryMinute << endl << endl;
        QuaranDiary << "[Health Update]\n\n" << diary[i].QuaranDiaryUpdate << endl;
        QuaranDiary << "-------------------------------------------------------------------------------------" << endl;
    }


    if (QuaranDiary.is_open())
    {
        cout << "\nCheck QuaranDiary.txt for your written health updates!" << endl;
    }

    else
    {
        cout << "\n\nUnable to Open File!" << endl;
    }

    QuaranDiary.close();
}
评论