- C++ Basics
Mar 06, 2018 The instructions here are as per version 4.9.9.2 of Dev C. I don’t think there will be much changes in any older version. So here we go: 1. Go to the Tools menu. In the Tools menu, you should find and option called Compiler Options. Dec 26, 2017 There are two aspects to this. One is understanding that the C compiler needs references to functions and classes that you are using outside of your current module. I've read all the fstream tutorials, and none of the code works in devc 4.9.9.2. For example here is a function I'm working on. The code I have to convert a string to char works.
- C++ Object Oriented
- C++ Advanced
- C++ Useful Resources
- Selected Reading
So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively.
This tutorial will teach you how to read and write from a file. This requires another standard C++ library called fstream, which defines three new data types −
Sr.No | Data Type & Description |
---|---|
1 | ofstream This data type represents the output file stream and is used to create files and to write information to files. |
2 | ifstream This data type represents the input file stream and is used to read information from files. |
3 | fstream This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files. |
To perform file processing in C++, header files <iostream> and <fstream> must be included in your C++ source file.
Opening a File
A file must be opened before you can read from it or write to it. Either ofstream or fstream object may be used to open a file for writing. And ifstream object is used to open a file for reading purpose only.
C++ Fstream Write File
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
Sr.No | Mode Flag & Description |
---|---|
1 | ios::app Append mode. All output to that file to be appended to the end. |
2 | ios::ate Open a file for output and move the read/write control to the end of the file. |
3 | ios::in Open a file for reading. |
4 | ios::out Open a file for writing. |
5 | ios::trunc If the file already exists, its contents will be truncated before opening the file. |
You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case that already exists, following will be the syntax −
Similar way, you can open a file for reading and writing purpose as follows −
Closing a File
When a C++ program terminates it automatically flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
Writing to a File
While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object.
Reading from a File
You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
Read and Write Example
Following is the C++ program which opens a file in reading and writing mode. After writing information entered by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen −
When the above code is compiled and executed, it produces the following sample input and output −
Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.
File Position Pointers
Both istream and ostream provide member functions for repositioning the file-position pointer. These member functions are seekg ('seek get') for istream and seekp ('seek put') for ostream.
The argument to seekg and seekp normally is a long integer. A second argument can be specified to indicate the seek direction. The seek direction can be ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream.
The file-position pointer is an integer value that specifies the location in the file as a number of bytes from the file's starting location. Some examples of positioning the 'get' file-position pointer are −
Fstream Dev C++
C++Language | ||||
Standard Library Headers | ||||
Freestanding and hosted implementations | ||||
Named requirements | ||||
Language support library | ||||
Concepts library(C++20) | ||||
Diagnostics library | ||||
Utilities library | ||||
Strings library | ||||
Containers library | ||||
Iterators library | ||||
Ranges library(C++20) | ||||
Algorithms library | ||||
Numerics library | ||||
Input/output library | ||||
Localizations library | ||||
Regular expressions library(C++11) | ||||
Atomic operations library(C++11) | ||||
Thread support library(C++11) | ||||
Filesystem library(C++17) | ||||
Technical Specifications |
Language Support | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||
Concepts | |||||||||||||||||||||||||||||||||||||||||||||||||||
<concepts> | |||||||||||||||||||||||||||||||||||||||||||||||||||
Diagnostics | |||||||||||||||||||||||||||||||||||||||||||||||||||
General utilities | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||
Strings | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||
Localization | |||||||||||||||||||||||||||||||||||||||||||||||||||
Containers | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||
Iterators | |||||||||||||||||||||||||||||||||||||||||||||||||||
<iterator> | |||||||||||||||||||||||||||||||||||||||||||||||||||
Ranges | |||||||||||||||||||||||||||||||||||||||||||||||||||
<ranges> | |||||||||||||||||||||||||||||||||||||||||||||||||||
Algorithms | |||||||||||||||||||||||||||||||||||||||||||||||||||
Numerics | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||
Input/Output | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||
Regular expressions | |||||||||||||||||||||||||||||||||||||||||||||||||||
<regex> | |||||||||||||||||||||||||||||||||||||||||||||||||||
Filesystem support | |||||||||||||||||||||||||||||||||||||||||||||||||||
<filesystem> | |||||||||||||||||||||||||||||||||||||||||||||||||||
Thread support | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||
C compatibility | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
This header is part of the Input/Output library.
Classes | ||
implements raw file device (class template)[edit] | ||
implements high-level file stream input operations (class template)[edit] | ||
implements high-level file stream output operations (class template)[edit] | ||
implements high-level file stream input/output operations (class template)[edit] | ||
Typedefs | ||
filebuf | basic_filebuf<char> | |
wfilebuf | basic_filebuf<wchar_t> | |
ifstream | basic_ifstream<char> | |
wifstream | basic_ifstream<wchar_t> | |
ofstream | basic_ofstream<char> | |
wofstream | basic_ofstream<wchar_t> | |
fstream | basic_fstream<char> | |
wfstream | basic_fstream<wchar_t> | |
Functions | ||
(C++11) | specializes the std::swap algorithm (function template)[edit] | |
(C++11) | specializes the std::swap algorithm (function template)[edit] | |
(C++11) | specializes the std::swap algorithm (function template)[edit] | |
(C++11) | specializes the std::swap algorithm (function template)[edit] |