Ok so I have a couple of files within a C program diffrent files for diffrent things etc.

What I want to know is, Can I open all the files at the beginning of the program (in main) and close it when the program exits

or

Should I write the fopen at the beginning of the used functions and close them at the end of the functions?

Ok so I have a couple of files within a C program diffrent files for diffrent things etc.

What I want to know is, Can I open all the files at the beginning of the program (in main) and close it when the program exits

or

Should I write the fopen at the beginning of the used functions and close them at the end of the functions?

Whatever works.

If you call your functions many times, it might be more efficient to open the file once.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.