Formatted and Unformatted Console I/O Operations in C++ | C++ में Formatted और Unformatted Console I/O Operations

C++ provides different input/output operations to read data from the keyboard and display data on the monitor. These operations are broadly classified into two categories: Formatted Console I/O Operations and Unformatted Console I/O Operations.

C++ में Keyboard से Data प्राप्त करने और Monitor पर Data Display करने के लिए विभिन्न Input/Output Operations उपलब्ध होते हैं। इन Operations को मुख्य रूप से दो Categories में विभाजित किया जाता है: Formatted Console I/O Operations और Unformatted Console I/O Operations।

1. Formatted Console I/O Operations | फॉर्मेटेड कंसोल इनपुट/आउटपुट ऑपरेशन्स

Formatted console input/output operations allow us to control the format in which data is displayed or read. They help present data according to the user's requirements, such as specifying field width, filling empty spaces, and controlling decimal precision.

Formatted Console Input/Output Operations की सहायता से हम अपनी आवश्यकता के अनुसार Data को Display या Read करने के Format को नियंत्रित कर सकते हैं। इनकी सहायता से Field Width निर्धारित करना, खाली स्थानों को किसी Character से भरना तथा Decimal Precision नियंत्रित करना संभव होता है।

Some important formatted output functions and manipulators are described below.

कुछ महत्वपूर्ण Formatted Output Functions और Manipulators निम्नलिखित हैं।

1. width(int w)

The width() function specifies the minimum field width for the next formatted output value. If the value requires fewer characters than the specified width, the remaining positions are filled with spaces or the character specified by fill(). This setting applies only to the next formatted output field.

width() Function अगली Formatted Output Value के लिए Minimum Field Width निर्धारित करता है। यदि Value की लंबाई निर्धारित Width से कम होती है, तो शेष स्थान Spaces या fill() द्वारा निर्धारित Character से भरे जाते हैं। यह Setting केवल अगली Formatted Output Value पर लागू होती है।

Syntax:

cout.width(w);

Example:

#include <iostream>
using namespace std;
int main()
{
cout.width(10);
cout << 123;
return 0;
}

Output:

       123

Explanation | व्याख्या:

In this example, width(10) specifies a minimum field width of 10 characters for the next output value. Since 123 contains only three characters, the remaining seven positions are filled with spaces by default.

इस उदाहरण में width(10) अगली Output Value के लिए 10 Characters की Minimum Field Width निर्धारित करता है। चूँकि 123 में केवल तीन Characters हैं, इसलिए शेष सात स्थान Default रूप से Spaces से भर दिए जाते हैं।

2. fill(char ch)

The fill() function specifies the character used to fill unused positions in a formatted output field. The specified fill character remains active until it is changed.

fill() Function Formatted Output Field में खाली स्थानों को भरने के लिए Character निर्धारित करता है। निर्धारित Fill Character तब तक Active रहता है, जब तक उसे किसी दूसरे Character से बदला नहीं जाता।

Syntax:

cout.fill(ch);

Example:

#include <iostream>
using namespace std;
int main()
{
cout.fill('*');
cout.width(10);
cout << 123;
return 0;
}

Output:

*******123

Explanation | व्याख्या:

In this example, fill('*') sets the fill character to an asterisk (*). The width(10) function specifies a field width of 10 characters. Since the output value contains three characters, the remaining seven positions are filled with asterisks.

इस उदाहरण में fill('*') Fill Character को Asterisk (*) निर्धारित करता है। width(10) Function Field Width को 10 Characters निर्धारित करता है। चूँकि Output Value में तीन Characters हैं, इसलिए शेष सात स्थान Asterisk से भर दिए जाते हैं।

3. precision(int n)

The precision() function specifies the precision used when displaying floating-point values. By default, it controls the total number of significant digits, not just the digits after the decimal point. When used with the fixed manipulator, it specifies the number of digits after the decimal point.

precision() Function Floating-Point Values को Display करते समय Precision निर्धारित करता है। Default रूप से यह Significant Digits की कुल संख्या नियंत्रित करता है, न कि केवल Decimal Point के बाद के Digits की। जब इसका उपयोग fixed Manipulator के साथ किया जाता है, तब यह Decimal Point के बाद आने वाले Digits की संख्या निर्धारित करता है।

Syntax:

cout.precision(n);

Example:

#include <iostream>
using namespace std;
int main()
{
double value = 12.345678;
cout.precision(4);
cout << value << endl;
cout << fixed;
cout.precision(2);
cout << value;
return 0;
}

Output:

12.35
12.35

Explanation | व्याख्या:

In the first output, precision(4) specifies four significant digits. In the second output, fixed changes the formatting to fixed-point notation, and precision(2) displays two digits after the decimal point.

पहले Output में precision(4) चार Significant Digits निर्धारित करता है। दूसरे Output में fixed Fixed-Point Notation लागू करता है तथा precision(2) Decimal Point के बाद दो Digits Display करता है।

Formatted Output Using Manipulators | Manipulators द्वारा Formatted Output

The same formatting operations can also be performed using manipulators such as setw(), setfill(), setprecision(), and fixed. These manipulators are available through the <iomanip> header file, while endl is available through <iostream>.

इसी प्रकार के Formatting Operations setw(), setfill(), setprecision() और fixed जैसे Manipulators द्वारा भी किए जा सकते हैं। ये Manipulators <iomanip> Header File के माध्यम से उपलब्ध होते हैं, जबकि endl का उपयोग <iostream> के माध्यम से किया जा सकता है।

Example:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << setfill('*') << 123 << endl;
cout << fixed << setprecision(2) << 12.3456;
return 0;
}

Output:

*******123
12.35

Explanation | व्याख्या:

In this program, setw(10) sets the field width to 10 characters, while setfill('*') fills the unused positions with asterisks. The fixed and setprecision(2) manipulators display the floating-point value with two digits after the decimal point.

इस Program में setw(10) Field Width को 10 Characters निर्धारित करता है, जबकि setfill('*') खाली स्थानों को Asterisk से भरता है। fixed और setprecision(2) Manipulators Floating-Point Value को Decimal Point के बाद दो Digits के साथ Display करते हैं।

2. Unformatted Console I/O Operations | अनफॉर्मेटेड कंसोल इनपुट/आउटपुट ऑपरेशन्स

Unformatted console input/output operations read or write characters and character sequences directly, without applying the usual formatted data conversion and field-width formatting. They are useful when we need to read individual characters, complete lines, or a specified number of characters.

Unformatted Console Input/Output Operations Characters और Character Sequences को सामान्य Formatted Data Conversion तथा Field-Width Formatting के बिना Read या Write करते हैं। जब हमें Individual Characters, पूरी Lines या निश्चित संख्या में Characters Read करने होते हैं, तब ये Operations उपयोगी होते हैं।

Some important unformatted console I/O functions are described below.

कुछ महत्वपूर्ण Unformatted Console I/O Functions निम्नलिखित हैं।

1. get() Function

The get() function is used to read a character or a sequence of characters from the standard input stream. It provides different forms of input operations.

get() Function का उपयोग Standard Input Stream से एक Character या Characters के समूह को Read करने के लिए किया जाता है। यह Input प्राप्त करने के लिए विभिन्न Forms प्रदान करता है।

Common forms of get():

(a) Reading a single character

The get() function without arguments reads a single character and returns it. Its return type is int_type, which allows it to represent the special end-of-file value as well.

बिना Arguments वाला get() Function एक Character Read करता है और उसे Return करता है। इसका Return Type int_type होता है, जिससे यह End-of-File जैसी विशेष Value को भी दर्शा सकता है।

Syntax:

char ch;
ch = cin.get();

Example:

#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter a character: ";
ch = cin.get();
cout << "Entered character: ";
cout.put(ch);
return 0;
}

Output:

Enter a character: A
Entered character: A

Explanation | व्याख्या:

In this example, cin.get() reads a single character from the keyboard and stores it in variable ch. The character is then displayed using cout.put().

इस उदाहरण में cin.get() Keyboard से एक Character Read करता है और उसे Variable ch में Store करता है। इसके बाद उस Character को cout.put() की सहायता से Display किया जाता है।

(b) Reading characters into an array

The get() function can also read a sequence of characters into a character array. It reads up to the specified limit or until the newline character is encountered. Unlike getline(), this form of get() does not extract the newline character from the input stream.

get() Function की सहायता से Characters के समूह को Character Array में भी Read किया जा सकता है। यह निर्धारित Limit तक या Newline Character मिलने तक Characters Read करता है। getline() के विपरीत, get() का यह Form Newline Character को Input Stream से Extract नहीं करता।

Syntax:

cin.get(array_name, size);

Example:

#include <iostream>
using namespace std;
int main()
{
char name[30];
cout << "Enter your name: ";
cin.get(name, 30);
cout << "Name: " << name;
return 0;
}

Output:

Enter your name: Rahul Joshi
Name: Rahul Joshi

Explanation | व्याख्या:

In this example, cin.get(name, 30) reads characters into the array name, allowing a maximum of 29 characters plus the terminating null character. Reading stops when a newline is encountered or the size limit is reached.

इस उदाहरण में cin.get(name, 30) Characters को Array name में Read करता है। इसमें अधिकतम 29 Characters और अंत में Null Character Store किया जा सकता है। Newline मिलने या Size Limit पूरी होने पर Reading रुक जाती है।

2. put(char ch) Function

The put() function is used to display a single character on the standard output device. It is a member function of the output stream class.

put() Function का उपयोग Standard Output Device पर एक Character Display करने के लिए किया जाता है। यह Output Stream Class का Member Function है।

Syntax:

cout.put(ch);

Example:

#include <iostream>
using namespace std;
int main()
{
char ch = 'A';
cout.put(ch);
return 0;
}

Output:

A

Explanation | व्याख्या:

In this example, cout.put(ch) displays the character stored in variable ch on the monitor.

इस उदाहरण में cout.put(ch) Variable ch में Store Character को Monitor पर Display करता है।

3. getline() Function

The getline() function is used to read a complete line of characters from the standard input stream and store it in a character array. It reads characters until a newline character is encountered or the specified size limit is reached. The newline delimiter is extracted from the stream but is not stored in the array.

getline() Function का उपयोग Standard Input Stream से पूरी Line Read करके उसे Character Array में Store करने के लिए किया जाता है। यह Newline Character मिलने या निर्धारित Size Limit पूरी होने तक Characters Read करता है। Newline Delimiter Input Stream से Extract हो जाता है, लेकिन Array में Store नहीं होता।

Syntax:

cin.getline(array_name, size);

Example:

#include <iostream>
using namespace std;
int main()
{
char name[30];
cout << "Enter your full name: ";
cin.getline(name, 30);
cout << "Your name is: " << name;
return 0;
}

Output:

Enter your full name: Rahul Joshi
Your name is: Rahul Joshi

Explanation | व्याख्या:

In this example, cin.getline(name, 30) reads a complete line from the keyboard and stores it in the character array name. It can read spaces between words, unlike the usual cin >> name operation.

इस उदाहरण में cin.getline(name, 30) Keyboard से पूरी Line Read करके Character Array name में Store करता है। यह Words के बीच Spaces को भी Read कर सकता है, जबकि सामान्य cin >> name Operation ऐसा नहीं करता।

4. write() Function

The write() function is used to display a specified number of characters from a character array on the standard output device. It writes exactly the number of characters specified, provided those characters are available in the array.

write() Function का उपयोग Character Array से निर्धारित संख्या में Characters को Standard Output Device पर Display करने के लिए किया जाता है। यह निर्धारित संख्या के अनुसार Characters Write करता है, बशर्ते Array में उतने Characters उपलब्ध हों।

Syntax:

cout.write(array_name, number_of_characters);

Example:

#include <iostream>
using namespace std;
int main()
{
char name[] = "COMPUTER";
cout.write(name, 8);
return 0;
}

Output:

COMPUTER

Explanation | व्याख्या:

In this example, cout.write(name, 8) displays eight characters from the character array name on the monitor.

इस उदाहरण में cout.write(name, 8) Character Array name से आठ Characters को Monitor पर Display करता है।

Difference Between Formatted and Unformatted Console I/O Operations | Formatted और Unformatted Console I/O Operations में अंतर

Formatted I/OUnformatted I/O
Used to control the format of input and output data.Used to read or write characters and character sequences directly.
Supports field width and precision formatting.Does not apply the usual field-width and precision formatting.
Performs formatted data conversion.Performs character-oriented input/output operations.
Examples: width(), fill(), precision(), setw(), setprecision().Examples: get(), put(), getline(), write().
Useful for presenting data in a specified format.Useful for character and line-based input/output.

Formatted and unformatted console I/O operations are important features of C++ programming. Formatted operations help control the appearance of input and output data, while unformatted operations are useful for reading and writing individual characters, complete lines, and character sequences. Both types of operations make console input/output flexible and efficient.

Formatted और Unformatted Console I/O Operations C++ Programming की महत्वपूर्ण विशेषताएँ हैं। Formatted Operations Input और Output Data के Appearance को नियंत्रित करने में सहायता करते हैं, जबकि Unformatted Operations Individual Characters, पूरी Lines और Character Sequences को Read तथा Write करने के लिए उपयोगी होते हैं। दोनों प्रकार के Operations Console Input/Output को Flexible और Efficient बनाते हैं।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Syllabus - From Basics to Advanced C++ , including OOP and Data Structures and Algorithms

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)