Writing to single file in multiple functions

Hi all from a beginner,

I'm trying to print all my output from a program to a single text file (running in unix) (I tried the redirection ">" but didn't work)

I opened the file in main(), and written some output to it using fprintf(), then I called a function, opened the file again with
fopen("output.txt","a")
and tried to use fprintf again to append output to the same file, but when I run the program, only the output from main() were printed to the file, nothing was printed after the call to the second function.

Is it not feasible for you to declare a global file pointer for that file and open it first in the main function (before you start redirecting out put to it) and then use that same filepointer everywhere. You can open the file in the append mode, so you are sure the pointer will always be at the end and what ever you write into it will be appended at the end...

Is it not feasible for you to declare a global file pointer for that file and open it first in the main function (before you start redirecting out put to it) and then use that same filepointer everywhere.

Except that you are using global variables when you can easily just pass it around and have things be a lot safer.

Thanks all for helping out...
When I used the redirection operator >, the output are not in the correct order.
The structure of my program is like this:main() prints to output.txt, then calls a function solver(),solver() append output to output.txt, then use system("./generate > dev/null") to call another program generate,
Is it because I didn't declare the file globally?
or because I opened the file in main() (in write mode) and then opened it again in solver() (in append mode)?

When I used the redirection operator >, the output are not in the correct order.

Um hate to tell you but using redirection doesn't magically change the order of the output, all it does is takes the output to stdout and sends it to the file. If there is something wrong with the order of the output then I'd suggest you look at your program.

I don't know why, but when I run the program without the redirection operator, the output are in the correct order, but when I call the program with a redirection to a text file AND within the program a system call to another program also with a redirection to the same text file, the order changed...