10.3 Files (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Consider a text file where each line represents a word. Write pseudo-code to determine the frequency of each word in the file. Your pseudo-code should include steps to read the file, convert each word to lowercase, and store the word frequencies in a suitable data structure. The data structure should allow for efficient counting of occurrences.
Algorithm: Word Frequency Counter
Data Structure: A dictionary where the keys are the words (in lowercase) and the values are the counts of how many times each word appears.
- Start
- Open the text file for reading.
- Create an empty dictionary called
wordFrequencies
. - For each line in the file:
- Read the line.
- Convert the line to lowercase.
- Split the line into individual words using whitespace as a delimiter.
- For each word:
- If the word is already a key in
wordFrequencies
:- Increment the value (count) associated with the word by 1.
- Otherwise:
- Add the word as a new key to
wordFrequencies
with a value of 1.
- Print the
wordFrequencies
dictionary. This will show each word and its count. - Close the text file.
- End
2.
Describe the different types of file systems commonly used in operating systems. Compare and contrast at least two different file system types, highlighting their key characteristics, advantages, and disadvantages.
File systems are the methods an operating system uses to organize and store files on a storage device. Different file systems have varying characteristics that affect performance, reliability, and compatibility. Here's a comparison of two common file systems:
File System | Characteristics | Advantages | Disadvantages |
FAT32 | Older file system, widely supported. Limited file size (4GB). | Compatibility with older systems. | Limited file size, no journaling, less robust. |
NTFS | Modern file system, used by Windows. Supports large file sizes and journaling. | Large file support, journaling for data integrity, security features. | Limited compatibility with non-Windows systems. |
FAT32:
- Characteristics: FAT32 (File Allocation Table 32) is an older file system that was widely used in the past. It has a limitation on the maximum file size (4GB).
- Advantages: Its widespread compatibility makes it suitable for transferring data between different operating systems (Windows, macOS, Linux).
- Disadvantages: The 4GB file size limit is a significant drawback for modern applications that often deal with large files. It also lacks journaling, making it less robust in the event of system crashes.
NTFS:
- Characteristics: NTFS (New Technology File System) is the primary file system used by modern versions of Windows. It supports much larger file sizes and includes journaling.
- Advantages: NTFS offers better data integrity through journaling, supports large files, and provides advanced security features like file permissions.
- Disadvantages: NTFS has limited compatibility with non-Windows operating systems. While macOS can read NTFS volumes, writing to them often requires third-party software. Linux has varying levels of support, with some distributions offering native NTFS support.
3.
Describe the different ways files can be accessed and manipulated by a computer program. Explain the roles of file pointers, file descriptors, and input/output (I/O) operations in this process.
Computer programs access and manipulate files through a series of operations involving file pointers, file descriptors, and I/O operations. Here's a breakdown of these concepts:
- File Pointers: A file pointer is a variable that stores the current position within a file. It indicates where the next read or write operation should occur. The file system maintains the file pointer's location. When a read or write operation is performed, the file pointer is updated to reflect the new position.
- File Descriptors: A file descriptor is a non-negative integer that represents an open file. When a program opens a file, the operating system assigns it a unique file descriptor. This descriptor is used by the program to refer to the file. File descriptors are used by system calls to identify which file the operation should be performed on.
- Input/Output (I/O) Operations: I/O operations are the fundamental operations used to read data from and write data to files. Common I/O operations include:
- Opening a file: This establishes a connection between the program and the file, allocating a file descriptor.
- Reading from a file: This retrieves data from the file, moving the file pointer forward.
- Writing to a file: This stores data in the file, moving the file pointer forward.
- Closing a file: This releases the file descriptor, indicating that the program is finished with the file. It's crucial to close files to ensure data is properly written and resources are released.
Process Overview:
- The program requests to open a file from the operating system. The OS allocates a file descriptor and updates the file pointer.
- The program uses I/O operations (read, write) to interact with the file. Each operation updates the file pointer.
- When the program is finished, it closes the file. This releases the file descriptor and ensures that any buffered data is written to the disk.
Example: Consider a program that reads a text file. The program first opens the file, obtaining a file descriptor. It then uses a read operation to read data from the file, which advances the file pointer. The program continues reading until it reaches the end of the file. Finally, it closes the file, releasing the file descriptor.