0% found this document useful (0 votes)
4 views

Interactive File Handling Python vs CPP

The document provides a comprehensive comparison of file handling in Python and C++, covering concepts such as file creation, reading, writing, and modes for both languages. It highlights Python's ease of use and automatic file closing versus C++'s manual control and performance benefits for larger applications. Additionally, it discusses when to use each language based on task complexity and performance requirements.

Uploaded by

syedbaran438
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Interactive File Handling Python vs CPP

The document provides a comprehensive comparison of file handling in Python and C++, covering concepts such as file creation, reading, writing, and modes for both languages. It highlights Python's ease of use and automatic file closing versus C++'s manual control and performance benefits for larger applications. Additionally, it discusses when to use each language based on task complexity and performance requirements.

Uploaded by

syedbaran438
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

File Handling in Python vs C++

Interactive Beginner-Friendly Guide


What is File Handling?
• File handling allows programs to create, read,
write, or append data to files.

• Think of it like using a notebook:


• - Open the notebook (open file)
• - Write notes (write)
• - Read notes (read)
• - Add more notes (append)
Text vs Binary Files
• Text Files: Human-readable (.txt, .csv)
• Binary Files: Computer-readable (.exe, .jpg)

• Use text files for logs, data.


• Use binary files for images, media, compiled
code.
Python: Creating Files
• Code Example:
• file = open("file.txt", "w")
• file.write("Hello!")
• file.close()

• Use 'with' for better safety.


• with open("file.txt", "w") as file:
• file.write("Hello again!")
Python: Reading Files
• Read entire file:
• with open("file.txt", "r") as file:
• print(file.read())

• Read line-by-line:
• with open("file.txt") as file:
• for line in file:
• print(line.strip())
Python File Modes
• 'r' - Read
• 'w' - Write (overwrites)
• 'a' - Append
• 'r+' - Read/Write

• Use binary by adding 'b' like 'rb', 'wb'


C++: Creating Files
• #include <fstream>
• ofstream file("file.txt");
• file << "Hello!";
• file.close();

• ofstream = output file stream


C++: Reading Files
• #include <fstream>
• ifstream file("file.txt");
• string line;
• while (getline(file, line)) {
• cout << line << endl;
• }
C++ File Modes
• ios::in - Read
• ios::out - Write
• ios::app - Append
• ios::binary - Binary mode

• Example:
• ofstream file("data.txt", ios::app);
Python vs C++
• Python:
• - Easy and short code
• - Automatic file closing
• - Ideal for scripts

• C++:
• - Manual control
• - Faster for large programs
• - Used in system software
When to Use Each?
• Use Python for:
• - Simple tasks
• - Automation, logs

• Use C++ for:


• - Large apps
• - Performance-heavy code
• - Embedded systems
Quick Quiz: Which Mode Appends Data to a
File?

• A) 'r' mode
B) 'w' mode
C) 'a' mode
D) 'r+' mode
Line-by-Line File Reading: Python vs C++
• C++: • ✔ Python is more
• while(getline(file, line)) { concise
• cout << line << endl; • ✔ 'with' auto-
• } closes the file
• ✔ 'strip()'
• Python:
removes newline
• with open("myfile.txt", "r") as characters
file:
• for line in file:
• print(line.strip())
Writing to Text Files: Python vs C++
• Python:
• with open("file.txt", "w") as f:
• f.write("Hello, World!\n")
• C++:
• #include <fstream>
• ofstream file("file.txt");
• file << "Hello, World!" << endl;
• file.close();
Appending to Text Files: Python vs C++

• Python:
• with open("file.txt", "a") as f:
• f.write("Appended Line\n")
• C++:
• ofstream file("file.txt", ios::app);
• file << "Appended Line" << endl;
• file.close();
Writing to Binary Files: Python vs C++

• Python:
• with open("file.bin", "wb") as f:
• f.write(b"\x48\x65\x6C\x6C\x6F") # 'Hello'
• C++:
• #include <fstream>
• ofstream file("file.bin", ios::binary);
• char data[] = "Hello";
• file.write(data, sizeof(data));
• file.close();
Reading Binary Files: Python vs C++
• Python:
• with open("file.bin", "rb") as f:
• data = f.read()
• print(data)
• C++:
• ifstream file("file.bin", ios::binary);
• char data[6];
• file.read(data, sizeof(data));
• cout.write(data, sizeof(data));
• file.close();
Binary File Handling (Python)
• 🔡 Characters: • 🔢 Numbers ↔ Bytes:
• - Get ASCII value of char: • - int → bytes: struct.pack('i',
ord('A') → 65 123)
• - Get char from ASCII: chr(65) • - bytes → int: struct.unpack('i',
→ 'A' data)[0]
• - Hex of char: hex(ord('H')) →
'0x48' • 📎 Notes:
• - Always open binary files with
• 📝 Strings ↔ Bytes: 'rb' or 'wb'
• - Text to bytes: • - Use 'with open(...)' for safe
"Hello".encode() → b'Hello' file handling
• - Bytes to text:
b'Hello'.decode() → 'Hello'

You might also like