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?

Member Avatar for iamthwee

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.