Introduction To Streams in Computer Science
Introduction To Streams in Computer Science
Streams in
Computer
Science
Streams are sequences of bytes used for reading and writing data to
and from files. They are fundamental to input/output operations in
computer science.
Types of Streams: Text Streams and
Binary Streams
Text Streams Binary Streams
Text streams are designed for human-readable data, like Binary streams are for raw data, like images, audio, or
text files. Each character in a text stream is represented videos. Bytes in a binary stream can represent any value,
by a single byte. making them more flexible.
Input/Output
Operations on Text
Files
1 Reading Text Files 2
Writing to Text Files
Text files are read line-by- Data written to a text file is
line, with each line converted to strings and
interpreted as a string. stored line by line.
3 4 Error Handling
File Handling Functions
Specialized functions Functions are implemented
handle file operations like to check for errors during
opening, closing, reading, file operations, ensuring
writing, and seeking. data integrity.
Opening and Closing
Text Files
1 Opening
The file is opened and a connection is established
between the program and the file.
2 File Mode
The mode specifies how the file will be used, such as for
reading, writing, or appending.
3 Closing
The file connection is closed, releasing the resources
and saving any changes made to the file.
Reading and Writing Text Files
Reading Writing Error Handling
Data is read from the file, line by Data from memory is written to Error handling mechanisms
line, and stored in memory for the file, line by line, in the prevent data corruption and
processing. specified format. ensure successful file operations.
Input/Output
Operations on Binary
FilesReading Binary Files
Binary files are read byte by byte, providing access to
the raw data.
File Mode Specifies how the file will be accessed, typically in "rb"
(read binary) or "wb" (write binary) mode.