Topic :Sequential Files Example: Get() | Getline() | Insertion Operator



Get() function:

This function Get Input Character by character including spaces, tabs, and newline upto size and assign to the variable. This function is useful for limited number of characters sent to a specific variable. This function needs two arguments, 1st is char array and other is size of Array. This functio can take an optional Parameter which is delimeter. ('\n' by default).

Basic syntax to use this function is given below.


char array_name[size];
get(array_name, size);

Let's have a simple example to know, how we can use get() function to get Input character in our C++ program.


/* Writing to files using get() function: InfoBrother:*/
 
#include<iostream>//Required for input & output: 
#include<fstream>//Required for File function: 
using namespace std;
 
int main()
{
	ofstream myFile; //Object of type ofstream.
	char name[30];   //arguments - variable name & array of char.
	int age;
 
	cout<<" Enter Your Name: ";
	cin.get(name, 30);   //get function.
	cout<<" Enter Your Age: ";
	cin>>age;
 
	myFile.open("InfoBrother.txt", ios::out); //Open the File:
	if(myFile) //if File open Successfully:
	{
    	   myFile<<name<<endl;
    	   myFile<<age<<endl;
    	   cout<<" Data Stored Successful: "<<endl;
	}     
 
	//if any error while opening file:
	else cout<<" Error Occurred while Opening File: "<<endl;
 
	return 0;
}


getline() function:

This function is similar to get() function, but it read whole line of text that end with newline character, this function need two parameter, first one is Array of char, and the second one is size of that array. This function can take an Optional parameter which is a delimeter. ('\n' by default).

Basic syntax to use this function is given below.


char array_name[size];
getline(array_name, size);

Let's have a simple example to know, how we can use getline() function to get Input line in our C++ program.


/* Writing to files using getline() function: InfoBrother:*/
 
#include<iostream>//Required for input & output:
#include<fstream>//Required for File function:
using namespace std;
 
int main()
{
	ofstream myFile; //Object of type ofstream.
	char str[100];   //Arguments - Array name & size.
 
	cout<<" Enter Your text here: ";
	cin.getline(str, 100);   //getline() function.
 
        //Open the File:
	myFile.open("InfoBrother.txt", ios::out); 
	if(myFile) //if File open Successfully:
	{
    	    myFile<<str<<endl;
    	    cout<<" Data Stored Successful: "<<endl;
	}     
 
	//if any error while opening file:
	else cout<<" Error Occurred while Opening File: "<<endl;
 
	return 0;
}


Stream Insertion Operator:

Same line cin, we can fstream or ofstream to get data from user with insertion operator.

The basic syntax to use stream insertion operator is:


ofstream fp;  //fp is the object of type ofstream.
fp <<"some text"<<endl;
fp <<"some more text"<<endl;

Let's have a simple example to know, how we can use stream Insertion operator to get Input in our C++ program.


/* Writing to files using Insertion Operator:  InfoBrother:*/
 
#include<iostream> //Required for input & output:
#include<fstream>//Required for File function:
using namespace std;
 
int main()
{
	ofstream fp; //Object of type ofstream.
 
	fp.open("InfoBrother.txt", ios::out); //Open the File:
	if(fp) //if File open Successfully:
	{
	   fp<<"InfoBrother"<<endl;
	   fp<<"Sardar Omar"<<endl;
     	   fp<<"C++ Tutorial"<<endl;
     	   fp<<"Object Oriented Programming: "<<endl;
	}     
 
	//if any error while opening file:
	else cout<<" Error Occurred while Opening File: "<<endl;
 
	return 0;
}












I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement