As said by @jsulm and @ndt_mike , while doing file operations
Once u open the file either for reading the data or writing the data.
Use file.close(); .
Once u finished reading the data or writing the data, and for the next operations u can open the file and do the further necessary operations,then again use file.close();