Computer Science – File handling | e-Consult
File handling (1 questions)
Login to see all questions.
Click on a question to view the answer
Several file access modes are available to programs, each with a specific purpose:
- Read (r): Allows the program to read data from the file. The file must exist.
Example: Opening a text file to display its contents to the user. - Write (w): Allows the program to write data to the file. If the file exists, its contents are overwritten. If the file doesn't exist, a new file is created.
Example: Creating a new log file and writing application messages to it. - Append (a): Allows the program to write data to the end of the file. If the file doesn't exist, a new file is created.
Example: Adding new entries to a log file without overwriting existing data. - Read and Write (r+): Allows the program to both read from and write to the file. The file must exist.
Example: Opening a file to read some data, modify it, and then save the changes back to the same file. - Append and Read (a+): Allows the program to both append data to the end of the file and read from it. If the file doesn't exist, a new file is created.
Example: Adding new data to a log file and then immediately reading the entire file to display the latest entries.
Choosing the correct mode is crucial to avoid accidentally overwriting data or encountering errors.