File Basic
File Basic
## Table of Contents
1. [Introduction to File Handling](#introduction-to-file-handling)
2. [Streams in C++](#streams-in-c++)
3. [File Operations](#file-operations)
4. [File Opening Modes](#file-opening-modes)
5. [Text vs Binary Files](#text-vs-binary-files)
6. [File Pointers and Random Access](#file-pointers-and-random-access)
7. [Error Handling](#error-handling)
8. [Practical Examples](#practical-examples)
9. [Common Exam Problems](#common-exam-problems)
---
---
## Streams in C++
---
## File Operations
1. **Using Constructor**
```cpp
ofstream outFile("data.txt"); // opens for writing
ifstream inFile("data.txt"); // opens for reading
```
---
| Mode | Description |
|------|-------------|
| `ios::in` | Open for reading (default for ifstream) |
| `ios::out` | Open for writing (default for ofstream) |
| `ios::app` | Append to end of file |
| `ios::ate` | Open and seek to end |
| `ios::trunc` | Truncate file if exists |
| `ios::binary` | Open in binary mode |
| `ios::nocreate` | Open fails if file doesn't exist |
| `ios::noreplace` | Open fails if file exists |
**Combining modes**:
```cpp
file.open("data.txt", ios::out | ios::app | ios::binary);
```
---
### Comparison
| Aspect | Text File | Binary File |
|--------|-----------|-------------|
| Format | Human-readable | Machine-readable |
| Size | Larger (stores as text) | Smaller (stores raw data) |
| Portability | More portable | May have platform issues |
| Access | Sequential | Random access possible |
| Functions | `<<`, `>>`, `get()`, `put()` | `read()`, `write()` |
---
**Examples**:
```cpp
file.seekg(10); // Move to 10th byte from start
file.seekg(-5, ios::cur); // Move back 5 bytes
file.seekg(0, ios::end); // Move to end
```
---
## Error Handling
**Example**:
```cpp
while (file.good()) {
// Read file
}
if (file.eof()) {
cout << "End of file reached";
}
```
---
## Practical Examples
int main() {
ofstream out("data.txt");
if (!out) {
cout << "Error opening file";
return 1;
}
int main() {
ifstream in("data.txt");
if (!in) {
cout << "Error opening file";
return 1;
}
string line;
while (getline(in, line)) {
cout << line << endl;
}
in.close();
return 0;
}
```
// Writing
Person p = {"John", 25};
ofstream out("person.dat", ios::binary);
out.write((char*)&p, sizeof(p));
out.close();
// Reading
Person p2;
ifstream in("person.dat", ios::binary);
in.read((char*)&p2, sizeof(p2));
cout << p2.name << " " << p2.age;
in.close();
```
---
class Student {
int roll;
char name[30];
float marks;
public:
void getData() {
cout << "Enter roll, name, marks: ";
cin >> roll >> name >> marks;
}
void display() {
cout << roll << "\t" << name << "\t" << marks << endl;
}
int getRoll() { return roll; }
};
int main() {
fstream file("students.dat", ios::binary | ios::in | ios::out | ios::app);
Student s;
char ch;
do {
s.getData();
file.write((char*)&s, sizeof(s));
cout << "Add more? (y/n): ";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
file.close();
return 0;
}
```
---
These comprehensive notes cover all essential aspects of C++ file handling for your exam
preparation, combining theoretical concepts with practical examples and common exam
problems.