C++ I/O Operations

Introduction to C++ I/O Operations

जैसा की आप जानते है हर program में कुछ input दिया जाता है और वह program इस input को लेकर output generate करता है। कोई भी program Input -> Processing -> Output के model पर काम करता है। ये हर programming language में होता है।

अभी तक आपने keyboard के माध्यम से programs में input दिया है और monitor के माध्यम से आपने output देखा है। Keyboard एक standard input device और monitor एक standard output device होता है। लेकिन इन दोनों के अलावा C++ में आप दूसरे sources (disk files or other electrical devices) के द्वारा input ले सकते है और output भी show करवा सकते है।

हर तरह के environment में C++ को आसानी से यूज़ करने के लिए आपको सभी तरह से input और output को process करना आना चाहिए। इसलिए C++ के द्वारा provide किये गए input और output sources के बारे में आपको जानकारी होना आवश्यक है।

C++ में streams और stream classes के द्वारा I/O operations perform किये जाते है। इनके बारे आगे detail से बताया जा रहा है।

C++ Streams

C++ का I/O system कई devices (terminals,files और tape drives आदि) के साथ काम कर सकता है। हर device अलग होता है और अलग तरह से काम करता है। हर device के लिए individual mechanism provide करना संभव नहीं है। इसलिए C++ आपको एक common interface provide करती है जिसे stream कहते है।

Stream सभी devices से independent होती है और हर तरह के device के लिए एक ही तरह से काम करती है। एक stream input के समय device के द्वारा दिए गए input को bytes में convert करके program तक पहुँचाती है और output के समय program के द्वारा generate किये गए output को bytes में convert करके device तक पहुँचाती है। इसलिए stream को sequence of bytes भी कहा जाता है।

एक stream या तो किसी source की तरह काम करती है जिससे input लिया जाता है या फिर एक destination की तरह काम करती है जिसको output pass किया जाता है। जो stream program को input provide करती है वह input stream कहलाती है। और जो stream program से output प्राप्त करती है वह output stream कहलाती है। आसान भाषा में कहा जाये तो program input stream से data खींचता है और इसे process करने के बाद output stream में उड़ेल देता है।

C++ I/O operations in Hindi

C++ Stream Classes

C++ आपको कुछ classes की hierarchy provide करती है जिनके माध्यम से आप different input और output mediums को handle कर सकते है। इन्हें stream classes कहा जाता है। इनके बारे में नीचे detail से दिया जा रहा है।

ios

ये एक general input/output stream class होती है। इस class में वह functions होते है जो दूसरी input और output stream classes यूज़ करती है। इस class को 3 classes inherit करती है।

  1. istream
  2. ostream
  3. streambuf

istream

ये input stream class होती है और ios class की properties को inherit करती है। इस class में get(), getline() और read() जैसे input functions पाए जाते है। इस class में right shift operator को overload किया गया है।

ostream

ये output stream class होती है और ये भी ios class की properties को inherit करती है। इस class में put() और write() जैसे functions पाए जाते है। साथ ही इस class में left shift operator को overload किया गया है।

streambuf

ये class buffers के द्वारा physical devices के लिए interface provide करती है। यह filebuf class की base class होती है।

iostream

ये input/output stream class होती है। ये class ios, istream और ostream तीनों classes को ही inherit करती है। इस class में सभी input/output functions पाए जाते है। इसीलिए अब तक आप हर program में इसकी header file को add करते आये है।

cin & cout

अभी तक आप cin और cout को बहुत से programs में यूज़ कर चुके है। ये input stream class और output stream class के objects होते है। इनके द्वारा input/output perform करने के लिए « और » operators को overload किया गया है। Right shift (») operator को istream class में overload किया गया है और left shift («) operator को ostream class में overload किया गया है।

Input read करने का general syntax नीचे दिया जा रहा है।

cin >> var 1 >> var 2 >> var N;

जब ये statement execute होता है तो program का execution रुक जाता है और program input data के लिए wait करता है। एक बात आपको ध्यान रखनी चाहिए की default input device keyboard इसलिए यँहा पर keyboard की key press के लिए program wait करता है और values को appropriate locations में store करता है।

Output display करने का general syntax नीचे दिया जा रहा है।

cout << var1<<"string"<<var2<<var3;

जब ये statement execute होगा तो इसमें दिए गए सभी variables की values और string monitor पर display होते है। Monitor default output device होता है। इस statement में सभी strings को inverted commas ('') के अंदर लिखा जाता है।