What is Files in C plus plus With Examples

What is Files in C plus plus With Examples
What are Files in C plus With Examples 

What are Files in C plus With Examples 

File Handling in C++:A File handling is a mechanism to store the output of a program into a file and read from a file on the disk permanently.

fstream fstream header file is used to perform file operation in C++ this header file provides many classes like (ofstream,ifstream,fstream) to read from a file and write into a file.

👇👇👇👇👇

C File Handling

ofstream:ofstream file data type represents the output file stream and is used to create files and to write information to files.

ifstream: ifstream data type represents the input file stream and is used to read information from files.

fstream: fstream 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.

👇👇👇👇👇


File Operations in C++

1. Opening of file.

2. Writing into a file.

3. Appending data into a file.

4. Reading from a file.

5. Closing of file.

File Opening Modes: A open File mode can be open in a different mode to perform read and write operations on a file.

open() function is used to open a file,open function takes two arguments.

open(const char *filename, ios::openmode mode);



Different Types of file opening modes are given below.

1.ios::in this mode can be used Open a file for reading.

2.ios::out this mode can be used Open a file for writing.

3.ios::app this mode can be used Appends data to the end of the file.

4.ios::ate this File pointer moves to the end of the file but allows to writes data in any location in the file.

5.ios::binary this mode can be used Binary File.

6.ios::trunc
 this mode can be used Deletes the contents of the file before opening.



How to Writing files in C++?

#include<iostream> 

#include<fstream> 

using namespace std; 

int main()

ofstream profiles; 

ofiles.open("icoderweb.txt"); 

ofiles<< "icoderweb Website"<<endl; 

ofiles<< "This Website Learning Programming Language"<< endl; 

ofiles.close(); 

return 0; 


How To Reading file in C++?

#include<iostream>

#include<fstream>

using namespace std;

int main()

char str[200]; 

ifstream ifile;

ifile.open("icoderweb.txt")

cout << "Content of icoderweb.txt files Data is given below :"<<endl;

while (!ifile.eof())

{

ifile.getline(str, 200);

cout << str << endl; 

ifile.close(); 


How To Use Count number of the alphabet in a file?

#include<iostream>

#include<fstream>

using namespace std; 

int main() 

ifstream fin("icoderweb.txt"); 

char ch; 

int i;

int a=0; 

while(fin) 

fin.get(ch); 

i=ch; 

if((i >=65 && i <=90) || (i >=97 && i <=122)) 

a++; 

cout<<"Number of Alphabtet in icoderweb.txt File : "<<a<<endl; 


How To Use Count number of digits in a file

#include<iostream>

#include<fstream>

using namespace std; 

int main() 

ifstream fin("icoderweb.txt"); 

char ch; 

int i;

int d=0; 

while(fin) 

fin.get(ch); 

i=ch; if(i >=48 && i<=57) 

d++; 

cout<<" Number of Digits in icoderweb.txt File : "<<d<<endl; 

}


How To Use Count number of special symbols in a file?

#include<iostream>

#include<fstream>

using namespace std; 

int main() 

ifstream fin("icoderweb.txt"); 

char ch; 

int i;

int s=0; 

while(fin) 

fin.get(ch);

i=ch; 

if((i >=65 && i <=90) || (i >=97 && i <=122))

else if(i >=48 && i<=57) 
 
else s++; 

cout<<"Number of special symbol in icoderweb.txt File :"<<s<<endl; 

}


Example Count number of space in a file.

#include<iostream>

#include<fstream>

using namespace std; 

int main() 

ifstream fin("icoderweb.txt"); 

char ch; 

int i;

space=0;

while(fin) 

fin.get(ch); 

i=ch; 

if(ch==' ') 

space++; 

cout<<"Number of space in icoderweb.txt File : "<<space<<endl; 




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.