Call file.close() to close an open file. This flushes any buffered data and releases the file handle back to the operating system for other programs to use. Files also close automatically when the fstream object goes out of scope.
This RAII behavior means you often don't need explicit close calls in well-structured code. Close files explicitly when you need to reopen them or when you want to ensure data is written before continuing.
Otherwise, let automatic destruction handle closing.