
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
File opening modes(r versus r+) in C++
File handling in programming languages is very important for the interaction of programming with the memory for accessing files and fetching data in it.
Using a program, you can read data from a file as well as write data to the file and do a lot more functions.
Here, we will see reading of data from a file.
In programming, before performing any operation you need to open the file. And there are multiple modes to open a file in the programming language. The access to the file is based on the mode it is opened with.
Here we will learn about the difference between two modes of opening file for reading files, these are r and r+.
Both are used for reading files in the program.
Syntax for opening a file :
FILE *fp;
fp = fopen( “filename.fileextension” , “mode” )
r mode for opening a file:
The r mode for opening file, opens files for reading only. If the file does not exist NULL character is returned.
Program to illustrate the opening of file:
Example
#include <stdio.h> #include <iostream> using namespace std; int main() { FILE* readFile; char ch; readFile = fopen("file.txt", "r"); while (1) { ch = fgetc(readFile); if (ch == EOF) break; cout<<ch; } fclose(readFile); }
Output −
Tutorials Point
r+ mode for opening a file:
The r+ mode for opening a file is similar to r mode but has some added features. It opens the file in both read and write mode. If the file does not exist with w+, the program creates new files to work on it.
Program to illustrate the opening of file in r+ mode:
Example
#include <stdio.h> #include <iostream> using namespace std; int main() { FILE* readFile; char ch; readFile = fopen("file.txt", "r+"); while (1) { ch = fgetc(readFile); if (ch == EOF) break; cout<<ch; } fclose(readFile); }
Output −
Tutorials Point
- Related Articles
- SAP Crystal Report file is not opening in VS2015
- Opening and reading a file with askopenfilename in Tkinter?
- What does opening a file actually do on Linux?
- How to Automatically Hide Specific Worksheets When Opening an Excel File?
- How do we specify the buffer size when opening a file in Python?
- What are the modes a file can be opened using Python?
- ADSL versus Cable
- Java String.equals() versus ==.
- MyISAM versus InnoDB in MySQL?
- calloc() versus malloc() in C
- Matching Versus Searching in Python
- For Versus While in C++
- For Versus While Loop in C
- Addressing modes in 8086 microprocessor
- Reinvestment Rate Assumption in NPV versus IRR
