When a program runs, the data is in the memory but when it ends or the computer shuts down, it gets lost. To keep data permanently, we need to write it in a file.
Dev C++ Download Windows 10
File is used to store data. In this topic, you will learn about reading data from a file and writing data to the file.
- Nov 29, 2016 Hansoft is the agile project management tool for enterprise teams. Fast, efficient, and flexible, Hansoft empowers teams to collaborate more efficiently so they can advance together.
- Nov 29, 2016 Delphi is the ultimate IDE for creating cross-platform, natively compiled apps. Are you ready to design the best UIs of your life? Our award winning VCL framework for Windows and FireMonkey (FMX) visual framework for cross-platform UIs provide you with the foundation for intuitive, beautiful.
fstream is another C++ standard library like iostream and is used to read and write on files.
These are the data types used for file handling from the fstream library:
Nov 14, 2019 You can build native C apps for iOS, Android, and Windows devices by using the cross-platform tools available in Visual Studio. Mobile development with C is a workload available in the Visual Studio installer. It installs the SDKs and tools you need for cross-platform development of shared libraries and native apps. Dec 01, 2011 Download C Programming Language Pro and enjoy it on your iPhone, iPad, and iPod touch. The classic C programming language for iPad, iPhone and iPod touch. Programming language is a perfect tool for studying, complex mathematical calculation, entertainment. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.
Data type | Description |
---|---|
ofstream | It is used to create files and write on files. |
ifstream | It is used to read from files. |
fstream | It can perform the function of both ofstream and ifstream which means it can create files, write on files, and read from files. |
Opening a file
We need to tell the computer the purpose of opening our file. For e.g.- to write on the file, to read from the file, etc. These are the different modes in which we can open a file.
Mode | Description |
---|---|
ios::app | opens a text file for appending. (appending means to add text at the end). |
ios::ate | opens a file for output and move the read/write control to the end of the file. |
ios::in | opens a text file for reading. |
ios::out | opens a text file for writing. |
ios::trunc | truncates the content before opening a file, if file exists. |
Let's look at the syntax of opening a file.
We have opened the file 'example.txt' to write on it. 'example.txt' file must be created in your working directory. We can also open the file for both reading and writing purposes. Let's see how to do this:
Dev C++ For Windows 10
Closing a file
C++ automatically close and release all the allocated memory. But a programmer should always close all the opened files. Let's see how to close it.
Dev C++ App For Pc
Reading and writing on a file
Dev C++ Iostream Download
We use << and >> to write and read from a file respectively. Let's see an example.