I have a project which requires me to print data in an output file using two functions. One function prints the values of a vector, and the other prints the values of an array. However, the second function that is called in main overwrites whatever the first function printed. I've tried opening the file in the first function and closing it in the second, but that didn't work. Apparently, when you move around from function to function, the write position is reset to the beginning of the file. However, I am unable to use seekp(); since we haven't actually covered that in class. Any insight as to how I should do this?
void writeToFile(vector<int> vec, int count, int average)
{
ofstream outFile;
outFile.open("TopicFout.txt");
// Prints all values of the vector into TopicFout.txt
outFile << "The values read are:" << endl;
for (int number = 0; number < count; number++)
outFile << vec[number] << " ";
outFile << endl << endl << "Average of values is " << average;
}
void writeToFile(int arr[], int count, int median, int mode, int countMode)
{
ofstream outFile;
// Prints all values of the array into TopicFout.txt
outFile << "The sorted result is:" << endl;
for (int number = 0; number < count; number++)
outFile << arr[number] << " ";
outFile << endl << endl << "The median of values is " << median << endl << endl;
outFile << "The mode of values is " << mode << " which occurs " << countMode << " times." << endl << endl;
outFile.close();
}
Use
outFile.open("TopicFout.txt", ios_base::app | ios_base::out);
instead of justoutFile.open("TopicFout.txt");