再次打开时,C ++会覆盖文本文件

时间:2012-03-19 19:59:52

标签: c++ text-files overwrite

该程序应该打开一个txt文件,记录两个不同问题的答案,关闭文件,然后在一个名为accumulatingFunction的函数中重新打开它,这样就可以计算每个问题的答案。

我设法让它工作但我的问题是当我关闭程序时会说,每个答案有3个实例,但是当我打开程序并添加每个问题的1个答案时它仍然会说我有3个答案的实例。

我假设它已经过去写了已经存在的答案之一,我不能为我的生活弄清楚如何让它停止。 (另外,现在accumulatingFunction只检查每个问题的第一个答案。我想确保在我添加其余部分之前这是可能的。或者我想也许你们会有另一种方法来做这个。)< / p>

#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();
}

2 个答案:

答案 0 :(得分:2)

使用in参数第二次打开文件。

所以改变这个

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

到这个

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

编辑:误读问题我以为你试图添加到文件中而不读它。

答案 1 :(得分:2)

您的问题是您未传递给fstream :: open的参数。您必须将fstream::out | fstream::app作为第二个参数传递。

另见the fstream::open reference

此外,由于您未在main()中阅读该文件,因此应使用ofstream代替fstream