Trying to write in one file using 2 functions

2.5k Views Asked by At

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

There are 2 best solutions below

2
On

Use outFile.open("TopicFout.txt", ios_base::app | ios_base::out); instead of just outFile.open("TopicFout.txt");

0
On

As Roger suggested on the comments, you can pass the ofstream to the functions using a pointer of by reference.

The simplest way should be passing it by reference. In this way you declare -and initialize if you want so- the ofstream on your main function:

ofstream outFile;               // declare the ofstream
outFile.open("TopicFout.txt");  // initialize
...                             // error checking         
...                             // function calls
outFile.close();                // close file
...                             // error checking 

Your first function may looks like:

void writeToFile(ofstream& outFile, vector<int> vec, int count, int average)
{
    // 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;

}

If you are using a C++11 conformant compiler, it also should be OK to pass the ofstream like that:

void writeToFile(std::ofstream outFile, vector<int> vec, int count, int average) {...}

Otherwise the copy constructor will be invoked, but there is no such definition for the ofstream class.