Files (Copy)
Introduction to Files
- Computer programs often need to store and retrieve data persistently.
- This is accomplished using files, which serve as external storage units for data.
- Each file is uniquely identified by a filename.
- This section primarily discusses text files, which contain sequences of characters.
- Text files may include an end-of-line (EOL) character, enabling structured reading and writing.
File Operations in Pseudocode
- File handling in pseudocode involves specific statements to open, read, write, append, and close files.
Opening a File
- Before reading from or writing to a file, it must be opened:
OPEN <file identifier> FOR <file mode> - Files can be opened in three modes:
- READ – Allows reading data from the file.
- WRITE – Writes new data to the file, replacing any existing content.
- APPEND – Adds new data at the end of the file without altering existing content.
Reading from a File
- Once a file is opened in READ mode, data can be read line by line using:
READFILE <file identifier>, <variable> - The retrieved line of text is stored in the variable.
Writing to a File
- A file opened in WRITE or APPEND mode can be written to line by line:
WRITEFILE <file identifier>, <variable> - Only STRING data type is allowed for reading and writing operations.
End-of-File (EOF) Function
- EOF function is used to determine whether the end of a file has been reached.
- It returns:
- TRUE – When the end of the file is reached.
- FALSE – If there is still more content to read.
EOF(<file identifier>)
Closing a File
- Once all file operations are complete, it must be closed:
CLOSEFILE <file identifier> - This ensures that the file is properly saved and no longer occupies system resources.
Example: Writing to and Reading from a File
- Consider a file named myText.txt.
- The following pseudocode demonstrates writing user input to the file and then reading it.
Writing to a File (Example)
DECLARE textLn : STRING
DECLARE myFile : STRING
myFile ← "myText.txt"
OPEN myFile FOR WRITE
REPEAT
OUTPUT "Please enter a line of text"
INPUT textLn
WRITEFILE myFile, textLn
UNTIL textLn = ""
CLOSEFILE myFile
Explanation
- The file is opened in WRITE mode.
- The program prompts the user to enter a line of text.
- Each line is written to the file using WRITEFILE.
- The loop continues until the user provides an empty input.
- The file is closed after writing.
Reading from a File (Example)
DECLARE textLn : STRING
DECLARE myFile : STRING
myFile ← "myText.txt"
OPEN myFile FOR READ
WHILE NOT EOF(myFile)
READFILE myFile, textLn
OUTPUT textLn
ENDWHILE
CLOSEFILE myFile
Explanation
- The file is opened in READ mode.
- A loop reads each line until the EOF is reached.
- Each line is displayed using OUTPUT.
- Finally, the file is closed.
File Modes Explained in Detail
| Mode | Function |
|---|---|
| READ | Opens the file for reading. Any attempt to write will result in an error. |
| WRITE | Opens the file for writing. Any existing data is overwritten. |
| APPEND | Opens the file for writing but does not erase existing data. New data is added to the end. |
Common File Handling Errors
- File Not Found Error: Occurs when trying to open a file that does not exist.
- Permission Denied Error: When attempting to write to a file without proper permissions.
- End-of-File (EOF) Unexpectedly Reached: Occurs when attempting to read past the last line of a file.
- Corrupted File Data: Can occur if the file is not properly closed before the program terminates.
Practical Applications of File Handling
- Configuration Files: Programs often store user preferences in text files.
- Log Files: Systems maintain logs to track events, errors, and user activity.
- Data Storage: Simple databases use text files to maintain records.
- User Input and Output: Programs can store user-provided text for later retrieval.
Advanced File Handling Concepts
Binary Files vs. Text Files
- Text Files:
- Store data as readable text characters.
- Includes newline characters for structured reading.
- Easily modified using text editors.
- Binary Files:
- Store data in machine-readable format (e.g., images, videos).
- More efficient for storage and processing.
- Not human-readable.
File Organization Methods
- Serial File Organization:
- Data is stored sequentially in the order it was added.
- Example: Logging events in an activity log.
- Sequential File Organization:
- Data is stored in a specific order, usually based on a key.
- Example: A sorted list of student records.
- Random File Organization:
- Data is stored at arbitrary locations, typically using hashing.
- Example: Database storage for fast lookup.
File Access Methods
- Sequential Access:
- Reads records one by one from the beginning.
- Suitable for log files or reports.
- Direct Access (Random Access):
- Allows jumping to a specific record without reading all previous records.
- Useful for databases.
Key Takeaways
- Files provide persistent storage for programs.
- Text files store sequences of characters and support structured reading and writing.
- Pseudocode operations for handling files include OPEN, READ, WRITE, APPEND, and CLOSE.
- The EOF function ensures proper reading of file content.
- Error handling is crucial to avoid issues like file corruption.
- Different file organization and access methods cater to varied data storage needs.
