---
title: "How to open a file in C++"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:file-open-syntax"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/file-open-syntax
site: BCT Engineering
---

# How to open a file in C++

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2074 Chaitra, 2 marks
- Video: [C++ File Handling | Creating and Opening | fstream, ifstream, ofstream | Video Tutorial](https://www.youtube.com/watch?v=rkJJzH-ymg8) — LearningLad
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/file-open-syntax
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

How can a file be opened in C++? Explain with suitable example and syntax.

## Short answer

A file is opened by creating an ifstream/ofstream/fstream object with the constructor (e.g., ofstreamfout('data.txt');) or by calling open(filename, mode). Modes include ios::in, out, app, binary, trunc, ate, combined with |.

## Answer

A file is opened in C++ by creating a file stream object (`ifstream` for input, `ofstream` for output, `fstream` for both) from `<fstream>` and linking it with a file. There are two ways.

#### 1. Using the constructor
```cpp
ofstream fout("data.txt");        // opens for writing
ifstream fin("data.txt");         // opens for reading
```

#### 2. Using the `open()` member function
Syntax: `stream_object.open("filename", mode);`

```cpp
#include <fstream>
#include <iostream>
using namespace std;
int main() {
    ofstream fout;
    fout.open("student.txt", ios::out | ios::app);   // append mode
    if (!fout) {
        cout << "File cannot be opened";
        return 1;
    }
    fout << "Ram 20" << endl;
    fout.close();
    return 0;
}
```

#### File modes
| Mode | Meaning |
|---|---|
| `ios::in` | Read |
| `ios::out` | Write (truncates existing file) |
| `ios::app` | Append at end |
| `ios::ate` | Go to end on opening |
| `ios::binary` | Binary mode |
| `ios::trunc` | Delete old contents |
| `ios::nocreate` / `ios::noreplace` | Fail if file does not / does exist (old compilers) |

Modes can be combined with `|`. `ifstream` uses `ios::in` and `ofstream` uses `ios::out` by default.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What are the two ways to open a file in C++?
   A. cin and cout
   B. new and delete
   C. fopen and fclose
   D. Constructor and open() member function
   Answer: D. Either pass the filename to the stream constructor or call open(). (Key point: Two ways)

2. Which mode adds data at the end of an existing file?
   A. ios::in
   B. ios::app
   C. ios::binary
   D. ios::trunc
   Answer: B. ios::app appends at the end; ios::out alone truncates. (Key point: Append)

3. How are file modes combined?
   A. With the | operator
   B. With &&
   C. With +
   D. With a comma
   Answer: A. Modes are combined with bitwise OR, e.g. ios::out | ios::app. (Key point: Combining modes)

## Related in other subjects

- [Engineer opens confidential salary file](https://www.bctengineer.com/question.md?key=semester_8%2Fengineering_professional_practice%3Aconfidential-salary-file-conduct) — Engineering Professional Practice, chapter 7
- [Line opened: stability and rotor swing](https://www.bctengineer.com/question.md?key=semester_5%2Fpower_system_analysis_ii%3Abreaker-a-opens-rotor-swing) — Power System Analysis II, chapter 6
- [INVENTORY file of items](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-inventory) — Computer Programming, chapter 10
