If you are writing in C, C++, Matlab or Java, this is how you proceed:
fprintf() takes multiple arguments, the first of which is the file handle, followed by the format/pattern, and the variables whose values are to be printed follow.
For example:
int year, rainfall;
...
FILE *f2=fopen("r.txt","w");
...
fprintf(f2,"%4d%10d\n", year, rainfall);
...
fclose(f2);
If rainfall is a float, then the %10d should be replaced by %10.2f meaning a width of 10 positions, and 2 places after the decimal. The \n at the end of the pattern tells the program to insert an end of line, so each year starts on a new line.
You will find plenty of references on the command fprintf if you google "fprintf", according to the language you are using.