Averaging outputs of each execution of a file
I think the short answer to your question is "what you're doing is fine, but there are other approaches depending on what you want to do." However, first:


When posting on CodeReview, try to post the most complete, compilable code that you can. This is often less work for you (because you can just paste straight from your source file with no editing), and less work for the reviewers (because they can just paste it straight into a source file and compile it). What you've posted here seems to be just the body of main.
Please indent your code consistently. This is good(ish):

if(file.is_open())
{
    while (!file.eof()) {
        file &gt;&gt; x;
        times.push_back(x);
    }
}


but this is bad:

if(write.is_open()) {
write &lt;&lt; counter &lt;&lt; endl;
};


You can use a tool like clang-format, astyle, or indent to format your code automatically until you acquire the muscle memory to do it yourself.




However, both of those snippets I just pasted have formatting "bugs" besides the indentation:  In the second one, you have a trailing ; that shouldn't be there.  In the first one, you mix brace styles:

if (x)
{


and

while(x) {


(Notice the mix of pre-parenthesis-spacing styles as well.)  Pick one style for a given program and stick with it.



Lastly, try to structure your program by breaking it down into subroutines. For this program, I'd probably have subroutines

// Generate a random number and return the number of tries for the user to guess it.
int guess_the_number();

// Append a number to the named file.
void append_to_file(const char *filename, int value);

// Retrieve the contents of the named file.
std::vector&lt;int&gt; load_ints_from_file(const char *filename);


And then I'd strongly consider refactoring the program along the lines suggested by symmetry: namely, would it make more sense to provide a function

// Retrieve the contents of the named file.
void save_ints_to_file(const char *filename, const std::vector&lt;int&gt;&amp; values);


instead of the append_to_file function?  There are valid reasons you might not want to in production code (for example, the append-only version has better efficiency and atomicity, if you do it right — which is not to say that you have), but if your goal is just to make a readable and sensible toy guess-the-number program, personally I'd go with the load/save dichotomy in place of the load/append dichotomy.



As for your question about "how to keep a running average": if all you care about is the average number of guesses (i.e. total number of guesses divided by total number of games played), then you don't need to keep a record of how the total number of guesses happened to divide up into individual games-played. Just store those two numbers in your file:

std::vector&lt;int&gt; values = load_ints_from_file("times.txt");
if (values.empty()) values = std::vector&lt;int&gt;{0, 0};
values[0] += guess_the_number();
values[1] += 1;
std::cout &lt;&lt; "Average is " &lt;&lt; values[0] / values[1] &lt;&lt; std::endl;
save_ints_in_file("times.txt", values);