File handling (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Describe the different file access modes available to a program. Explain the purpose of each mode and provide an example of when each mode would be used.
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.
2.
Explain why data is often stored in external files rather than directly in a program's memory. Consider the advantages in terms of memory usage, data persistence, and program flexibility.
Data is often stored in external files for several key reasons:
- Memory Usage: Storing data in files frees up valuable RAM. Programs can be large, and storing all data in memory would be expensive and potentially lead to performance issues, especially on devices with limited memory. Files allow for storing large datasets that don't need to be actively accessed all the time.
- Data Persistence: Data stored on a storage device (like a hard drive or SSD) persists even when the program is closed or the computer is turned off. This is crucial for applications that need to retain information between sessions, such as databases, documents, and settings. Data stored in memory is volatile and lost when power is lost.
- Program Flexibility: External files allow programs to be more modular and adaptable. Data can be updated or modified independently of the program's code. Different programs can also access the same data file, promoting data sharing and reusability. This separation of data and code makes maintenance and updates easier.
In summary, using external files provides a more efficient, persistent, and flexible way to manage data compared to storing it solely in a program's memory.
3.
Consider a scenario where a program needs to read data from a text file containing a list of student names, one name per line. Write pseudocode to demonstrate how the program would open the file, read each name, and then close the file. Assume the file is named "students.txt".
Pseudocode:
- Open file "students.txt" in read mode.
- Create a variable to store each student name (e.g., studentName).
- While the end of the file has not been reached:
- Read a line from the file and store it in studentName.
- Process the studentName (e.g., display it, store it in a list).
- Close file "students.txt".