簡體   English   中英

再次打開文本文件時,C ++會覆蓋它

[英]C++ overwrites on a text file when it is opened again

該程序應該打開一個txt文件,記錄來自兩個不同問題的答案,關閉該文件,然后在一個稱為accumulatingFunction的函數中重新打開它,以便可以計算每個問題的答案。

我設法使其正常工作,但是我的問題是,當我關閉程序時,每個答案將有3個實例,但是當我打開程序並為每個問題添加1個答案時,仍然會說有3個實例。

我以為它已經改寫了已經存在的答案之一,而我無法終生想出如何使它停止的方法。 (另外,現在,accumulatingFunction僅檢查每個問題的第一個答案。我想確保在添加其余問題之前可以做到這一點。或者我想也許你們將有另一種方法來這樣做。)

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

double userInput = 0;
string ethnicityQuestion();
void validationFunction(int);
string politicalQuestion();
void accumulatingFunction();



//-----------------------------------------------------------------------------------------------

int main()
{
    string ethnicityAnswer, politicalAffiliationAnswer, userID;
    fstream answerFile;

answerFile.open("F:\\midTermFile.txt");



if (!answerFile)
    cout << "You have a file read error" <<endl;


while (userID != "done")
{

ethnicityAnswer = ethnicityQuestion();
system("cls");

politicalAffiliationAnswer = politicalQuestion();
system("cls");


answerFile << ethnicityAnswer << endl;
answerFile << politicalAffiliationAnswer << endl;

cout << "you made it back to the main function and you chose " << ethnicityAnswer << " as your ethnicity\n"<< endl;
cout << "you made it back to the main function and you chose " << politicalAffiliationAnswer << " as your political affiliation\n"<< endl;

accumulatingFunction();

cout << "Please enter your user ID:  ";
cin >> userID;

}
answerFile.close();


return 0;
}

//-----------------------------------------------------------------------------------------------

string ethnicityQuestion()
{

    string ethnicity;
    int selection = 6;

string A = "Native_American";
string B = "Asian";
string C = "African American";
string D = "Hispanic/Latino";
string E = "Caucasion";
string F = "Other";

cout << "What ethnicity do you claim?\n";
cout << "1. Native American\n";
cout << "2. Asian\n";
cout << "3. African American\n";
cout << "4. Hispanic/Latino\n";
cout << "5. Caucasion\n";
cout << "6. Other\n";

validationFunction(selection);


if (userInput == 1)
    ethnicity = A;
else if (userInput == 2)
    ethnicity = B;
else if (userInput == 3)
    ethnicity = C;
else if (userInput == 4)
    ethnicity = D;
else if (userInput == 5)
    ethnicity = E;
else if (userInput == 6)
    ethnicity = F;

return ethnicity;
}

//------------------------------------------------------------------------------------------------

string politicalQuestion()
{
    string affiliation; 
    int selection = 6;

string A = "Very_Conservative";
string B = "Moderately Conservative";
string C = "Very Liberal";
string D = "Moderately Liberal";
string E = "Neither";
string F = "In the Middle";

cout << "On most political issues, which of the following do you associate with most:\n";
cout << "1. Very Conservative\n";
cout << "2. Moderately Conservative\n";
cout << "3. Very Liberal\n";
cout << "4. Moderatly Liberal\n";
cout << "5. Neither\n";
cout << "6. In the Middle\n";

validationFunction(selection);


if (userInput == 1)
    affiliation = A;
else if (userInput == 2)
    affiliation = B;
else if (userInput == 3)
    affiliation = C;
else if (userInput == 4)
    affiliation = D;
else if (userInput == 5)
    affiliation = E;
else if (userInput == 6)
    affiliation = F;

return affiliation;
}

//-----------------------------------------------------------------------------------

void validationFunction(int choiceAmount)
{
    while ((!(cin >> userInput)) || (userInput > choiceAmount || userInput < 1))
    {                       
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Please enter a number between 1 and 6: ";
    }
}

//------------------------------------------------------------------------------------------------

void accumulatingFunction()
{
    string userAnswer;
    double nativeAmerican = 0, veryConservative = 0;



    ifstream countFile;
    countFile.open("F:\\midTermFile.txt");



    while (!countFile.eof()) 
        {countFile >> userAnswer;

            if (userAnswer == "Native_American")
                nativeAmerican += 1;
            else if (userAnswer == "Very_Conservative")
                    veryConservative += 1;
            userAnswer = "";
        }
            cout << nativeAmerican << endl;
            cout << veryConservative << endl;

        countFile.close();
}

使用in參數第二次打開文件。

所以改變這個

countFile.open("F:\\midTermFile.txt");

對此

countFile.open("F:\\midTermFile.txt", fstream::in | fstream::app);

編輯:誤讀的問題,我認為您試圖添加到文件中而不是讀它。

您的問題是您沒有傳遞給fstream :: open的參數。 您必須通過fstream::out | fstream::app fstream::out | fstream::app作為第二個參數。

另請參見fstream :: open參考

另外,由於您不是從main()的文件中讀取數據,因此應使用ofstream而不是fstream

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM