File Handling in C++ (part-2)

    How To Work With File handling in C++?

    programming की दुनिया हो या न हो, Files महत्वपूर्ण हैं क्योंकि वे Data Store करती हैं। इस article में C++ हम file handling(फ़ाइल हैंडलिंग) के कार्य पर discuss करेंगे।

    इस Article में निम्नलिखित बिंदुओं को शामिल किया जाएगा,

    1. Opening a File (एक फ़ाइल खोलना)
    2. Writing to a File (एक फ़ाइल को लिखना)
    3. Reading from a File (एक फ़ाइल से पढ़ना)
    4. Close a File (एक फ़ाइल बंद करना)

    File Handling In C++

    storage device(स्टोरेज डिवाइस) में data को permanently स्टोर करने के लिए फाइलों का उपयोग किया जाता है। File Handling(फाइल हैंडलिंग) एक File में Program के आउटपुट को Store करने और उस पर विभिन्न Operation करने के लिए एक mechanism प्रदान करता है।

    एक stream एक abstraction है जो एक device का represents करती है जिस पर Input और Output का performe किया जाता है। एक stream को उसके उपयोग के आधार पर indefinite length के characters के source या destination के रूप में दर्शाया जा सकता है।

    C++ में हमारे पास File Handling(फाइल हैंडलिंग) methods का एक sets है। इनमें ifstream, ofstream और fstream शामिल हैं। ये Classes fstrembase और संबंधित iostream Class से ली गई हैं। disk files को manage करने के लिए design की गई इन classes को fstream में declared किया गया है और इसलिए हमें इस फ़ाइल को किसी भी program में include करना चाहिए जो फ़ाइलों का उपयोग करता है।

    File handling एक ऐसा mechanism   प्रदान करता है जिससे कि हम program के Output को file में store(स्टोर) कर सकते हैं और एक file में मौजूद data पर हम बहुत सारें operations को perform  कर सकते है। 

    C++ में, files में read और write operations को पूरा करने के लिए मुख्यतया तीन classes का Use किया जाता है:-

    1. ifstream
    2. ofstream
    3. fstream

    1. ofstream:- यह क्लास Output Stream को Represent करती है, इसका प्रयोग files को create करने के लिए और files में information  को write करने के लिए करते है। इस के बारे में हमने अपने last article में भी समझा था। 

    2. ifstream:-  यह क्लास input stream को Represent करती है, इसका प्रयोग files में से information या data को read करने के लिए किया जाता है। click here to more

    3. fstream:- यह क्लास output stream और input stream दोनों को Represent करती है. इसलिए यह files को create कर सकता है, file में डाटा को read कर सकता है और डाटा को write कर सकता है। 

    C++ में, file Processing को Perform करने के लिए program में header files <iostream> और <fstream> को include करना अनिवार्य होता है। 

    file opening

    फाइल में किसी भी operation को perform करने से पहले हमें file को open करना होता है। File को Open करने के लिए दो तरीके हैं। पहला Constructor(कंस्ट्रक्टर) के माध्यम से तथा दूसरा open() फंक्शन के माध्यम से इस Article मे file को open करने के लिए open() फंक्शन का प्रयोग करेंगे हैं। 

    Syntax:-

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

    file से data को read करना –

    हम C++ में Stream Extraction Operator (>>) करके files में से data को read कर सकते हैं।  हम इस Operator का प्रयोग उसी प्रकार कर सकते हैं जिस प्रकार हम keyboard से user input को read करते हैं। 

    file में data को write करना –

    हम File में Stream Insertion Operator (<<) का प्रयोग करके file में data को write कर सकते हैं।  file में लिखे जाने वाला text हमेशा Double Quotes के अंदर होना चाहिए। 

    file को close करना –

    Program को Terminate करने से पहले हमें सभी open की गयी files को close करना चाहिए. इसके लिए हम close() function का प्रयोग करते हैं। 

    Syntax:-

    void close();

    Different modes of opening:-

    यदि फाइल को ofstream या ifstream क्लास के माध्यम से खोला जाता है तो वह स्वतः write-only तथा read-only Mode में खुल जाती है। किंतु यदि फाइल को fstream क्लास के माध्यम से खोला जाए तो हमें यह बताना होता है कि फाइल को किस Mode में Open किया जाना है। File Open करते समय यह Mode बताने के लिए open() फंक्शन का प्रारूप निम्नानुसार है:-

    fileobj.open("filename", mode1 | mode2 | mode3 | ........);

    ios::in फाइल से डेटा read करने के लिए
    ios::out फाइल में डेटा write करने के लिए
    ios::binary बाइनरी फाइल बनाने हेतु
    ios::ateफाइल में डेटा जोड़ने (append) करने हेतु

    Note:-

    • ios::out मोड में यदि फाइल पहले से मौजूद होगी तो वह फाइल ओवरराइट हो जाएगी।
    • ofstream के लिए ios:: out तथा ifstream के लिए ios::in डिफाल्ट मोड होता है।

    Example:-


        #include <iostream>
        #include <fstream>
        #include <ctype.h>
        #include <conio.h>
        using namespace std;
        int main()
        {
            fstream file;
            file.open("hello.text", ios::out);
            char name[200];

            cout << "\nType multi-word string\n\n";
            cin.getline(name, 200);
            file << name;
            file.close();

            file.open("Hello.text", ios::in);
            file.getline(name, 200);

            cout << "\nStored string in the file is: ";        
            cout << "\n"
                << name;
            file.close();

            getch();
            return 0;
        }

    Output:-

    hello! Mukesh Singh Kabawat is a full Stack Developer and Author of W3 Coding Club. He Born on 12-March-2002 in Sanchor, Rajasthan. He is a Bachelor's in Computer Applications. He wrote articles about defferent type of programming language, Web Development, SEO, Ethical Hacking, Technology, on w3codingclub.com.

    Stored string in the file is:
    hello! Mukesh Singh Kabawat is a full Stack Developer and Author of W3 Coding Club. He Born on 12-March-2002 in Sanchor, Rajasthan. He is a Bachelor's in Computer Applications. He wrote articles abou

    Explian Example:-

    उपरोक्त उदाहरण में cin.getline (name, 200) के माध्यम से name नामक कैरेक्टर एरे में यूज़र से इनपुट लिया जा रहा है। यह इनपुट अधिकतम 200 अक्षरों तक हो सकता है, या यूज़र के कीबोर्ड से  Enter दबाने तक हो सकता है। इसी प्रकार file.getline(name, 200) के माध्यम से फाइल से डेटा read किया जा रहा है।

    1. Writing to a File

    Example:-


        // Writing to a File
        #include <iostream>
        #include <fstream>
        using namespace std;
        int main()
        {
            fstream new_file;
            new_file.open("new_file_write.txt", ios::out);
            if (!new_file)
            {
                cout << "File creation failed";
            }
            else
            {
                cout << "New file created";
                new_file << "Learning File handling"; // Writing to file    
                new_file.close();
            }
            return 0;
        }

    Output:-

    New file created

    Explain Example:-

    यहां हम पहले Open() Function का उपयोग करके एक New फ़ाइल "new_file_write" बनाते हैं क्योंकि हम फ़ाइल को आउटपुट भेजना चाहते हैं, इसलिए हमने ios :: out का उपयोग किया हैं। जैसा कि प्रोग्राम में दिया गया है, Insertion Pointer(इंसर्शन पॉइंटर) "<<" के आउटपुट फाइल में पास होने के बाद quotes के अंदर टाइप की गई जानकारी।

    ऊपर दिए गए Example में text फाइल कुछ इस तरह की बनेगी:-

    File Handling, w3 coding club, w3codigclub,

    2. Reading from a File

    Example:-


        //  Reading from a File
        #include <iostream>
        #include <fstream>
        using namespace std;
        int main()
        {
            fstream new_file;
            new_file.open("new_file_write.txt", ios::in);    
            if (!new_file)
            {
                cout << "No such file";
            }
            else
            {
                char ch;
                while (!new_file.eof())
                {
                    new_file >> ch;
                    cout << ch;
                }
                new_file.close();
                return 0;
            }
        }

    Output:-

    LearningFilehandlingg

    Explain Example:-

    इस उदाहरण में, हम उस File को Read कर रहे हैं हैं जो पिछले Example में create  की थी अर्थात new_file_write
    किसी फ़ाइल को read करने के लिए हमें Sytax ios::in के साथ 'in' Mode का उपयोग करना होगा। उपरोक्त उदाहरण में, हम extraction operator(>>) का उपयोग करके Files के  data को print  कर रहे  हैं। Output बिना किसी space के print होता है क्योंकि हम एक समय में केवल एक character का उपयोग कर रहे हैं, हमें पूरी लाइन को प्रिन्ट करने के लिए   getline () Function  का उपयोग करने की आवश्यकता होती है।

    Post a Comment

    0 Comments