Fortran में File Handling: फाइल से डेटा पढ़ें और लिखें

Fortran में File Handling: फाइल से डेटा पढ़ें और लिखें

Fortran में **File Handling** का उपयोग डेटा को इनपुट और आउटपुट फाइल्स से पढ़ने और लिखने के लिए किया जाता है। यह विशेष रूप से तब उपयोगी होता है जब आपको बड़े डेटा सेट्स के साथ काम करना हो या अपने प्रोग्राम के आउटपुट को एक फाइल में सहेजना हो। इस अध्याय में, हम सीखेंगे कि Fortran में फाइल्स को कैसे खोलना, पढ़ना, लिखना, और बंद करना है। इसके साथ ही, हम फाइल हैंडलिंग के विभिन्न तरीकों और Error Handling पर भी चर्चा करेंगे।

File Operations

Fortran में File Operations का उपयोग फाइलों से डेटा पढ़ने (reading), फाइलों में डेटा लिखने (writing), और फाइलों को खोलने (opening) और बंद (closing) करने के लिए किया जाता है। यह विशेष रूप से उपयोगी होता है जब आपको प्रोग्राम का इनपुट या आउटपुट फाइलों से लेना या देना हो। इस सेक्शन में, हम फाइल को खोलने, पढ़ने, लिखने, और बंद करने की प्रक्रिया पर चर्चा करेंगे।

1. Files को Open कैसे करें:

Fortran में फाइल को खोलने के लिए open स्टेटमेंट का उपयोग किया जाता है। open स्टेटमेंट के साथ फाइल नंबर (unit number) का उपयोग किया जाता है, जो फाइल को पहचाने का तरीका होता है।

Syntax:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
open(unit=unit_number, file='file_name', status='file_status')
open(unit=unit_number, file='file_name', status='file_status')
open(unit=unit_number, file='file_name', status='file_status')
  • unit_number: एक integer जो फाइल को पहचानने के लिए उपयोग किया जाता है।
  • file_name: फाइल का नाम (जिसे खोला जाना है)।
  • file_status: यह दर्शाता है कि फाइल पहले से मौजूद है या नहीं। (e.g., ‘old’, ‘new’, ‘replace’)।

उदाहरण:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
open(unit=10, file='input_data.txt', status='old')
open(unit=10, file='input_data.txt', status='old')
open(unit=10, file='input_data.txt', status='old')

यह उदाहरण input_data.txt नाम की फाइल को खोलता है, और यह मानता है कि फाइल पहले से मौजूद है (‘old’)। फाइल को पहचानने के लिए unit=10 का उपयोग किया गया है।

2. Files से डेटा कैसे पढ़ें (Read):

फाइल से डेटा पढ़ने के लिए Fortran में read स्टेटमेंट का उपयोग किया जाता है। आप फाइल से डेटा पढ़कर उसे वेरिएबल्स में स्टोर कर सकते हैं।

Syntax:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
read(unit=unit_number, *) variable_list
read(unit=unit_number, *) variable_list
read(unit=unit_number, *) variable_list
  • unit_number: फाइल के लिए उपयोग किया गया यूनिट नंबर।
  • variable_list: वह वेरिएबल्स जहाँ पढ़ा गया डेटा स्टोर किया जाएगा।
उदाहरण:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
program read_file_example
integer :: a, b
open(unit=10, file='input_data.txt', status='old')
! फाइल से डेटा पढ़ें
read(unit=10, *) a, b
print *, "a:", a, "b:", b
close(unit=10)
end program read_file_example
program read_file_example integer :: a, b open(unit=10, file='input_data.txt', status='old') ! फाइल से डेटा पढ़ें read(unit=10, *) a, b print *, "a:", a, "b:", b close(unit=10) end program read_file_example
program read_file_example
    integer :: a, b

    open(unit=10, file='input_data.txt', status='old')

    ! फाइल से डेटा पढ़ें
    read(unit=10, *) a, b
    print *, "a:", a, "b:", b

    close(unit=10)
end program read_file_example

स्पष्टीकरण: यह प्रोग्राम input_data.txt से दो integer मान (a और b) पढ़ता है और उन्हें स्क्रीन पर प्रिंट करता है।

3. Files में डेटा कैसे लिखें (Write):

फाइल में डेटा लिखने के लिए write स्टेटमेंट का उपयोग किया जाता है। आप एक या अधिक वेरिएबल्स का डेटा फाइल में स्टोर कर सकते हैं।

Syntax:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
write(unit=unit_number, *) variable_list
write(unit=unit_number, *) variable_list
write(unit=unit_number, *) variable_list
  • unit_number: वह यूनिट नंबर जो फाइल से जुड़ा हुआ है।
  • variable_list: वे वेरिएबल्स जिनका डेटा फाइल में लिखा जाएगा।
उदाहरण:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
program write_file_example
integer :: a, b
a = 5
b = 10
open(unit=20, file='output_data.txt', status='new')
! फाइल में डेटा लिखें
write(unit=20, *) a, b
close(unit=20)
end program write_file_example
program write_file_example integer :: a, b a = 5 b = 10 open(unit=20, file='output_data.txt', status='new') ! फाइल में डेटा लिखें write(unit=20, *) a, b close(unit=20) end program write_file_example
program write_file_example
    integer :: a, b

    a = 5
    b = 10

    open(unit=20, file='output_data.txt', status='new')

    ! फाइल में डेटा लिखें
    write(unit=20, *) a, b

    close(unit=20)
end program write_file_example

स्पष्टीकरण: इस प्रोग्राम में, a और b के मान output_data.txt में लिखे जाते हैं। अगर फाइल पहले से मौजूद नहीं है, तो status='new' के कारण एक नई फाइल बनाई जाएगी।

4. Files को Close कैसे करें:

फाइल को बंद करने के लिए close स्टेटमेंट का उपयोग किया जाता है। यह स्टेटमेंट फाइल को सही तरीके से बंद करता है और यूनिट नंबर को मुक्त करता है।

Syntax:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
close(unit=unit_number)
close(unit=unit_number)
close(unit=unit_number)
  • unit_number: वह यूनिट नंबर जो फाइल के लिए उपयोग किया गया है।
उदाहरण:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
close(unit=10)
close(unit=10)
close(unit=10)

यह फाइल को बंद करता है जो यूनिट नंबर 10 से जुड़ी हुई थी।

5. फाइल हैंडलिंग का एक पूर्ण उदाहरण:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
program file_handling_example
integer :: a, b
! Open input file to read data
open(unit=10, file='input_data.txt', status='old')
! Read data from file
read(unit=10, *) a, b
print *, "पढ़ा गया डेटा: a =", a, "b =", b
! Close the input file
close(unit=10)
! Open output file to write data
open(unit=20, file='output_data.txt', status='replace')
! Write data to the file
write(unit=20, *) a, b
! Close the output file
close(unit=20)
end program file_handling_example
program file_handling_example integer :: a, b ! Open input file to read data open(unit=10, file='input_data.txt', status='old') ! Read data from file read(unit=10, *) a, b print *, "पढ़ा गया डेटा: a =", a, "b =", b ! Close the input file close(unit=10) ! Open output file to write data open(unit=20, file='output_data.txt', status='replace') ! Write data to the file write(unit=20, *) a, b ! Close the output file close(unit=20) end program file_handling_example
program file_handling_example
    integer :: a, b

    ! Open input file to read data
    open(unit=10, file='input_data.txt', status='old')

    ! Read data from file
    read(unit=10, *) a, b
    print *, "पढ़ा गया डेटा: a =", a, "b =", b

    ! Close the input file
    close(unit=10)

    ! Open output file to write data
    open(unit=20, file='output_data.txt', status='replace')

    ! Write data to the file
    write(unit=20, *) a, b

    ! Close the output file
    close(unit=20)
end program file_handling_example

स्पष्टीकरण: इस प्रोग्राम में:

  • सबसे पहले, input_data.txt से डेटा पढ़ा जाता है और स्क्रीन पर प्रिंट किया जाता है।
  • फिर, उसी डेटा को output_data.txt में लिखा जाता है।
  • फाइल्स को खोलने, पढ़ने, लिखने, और बंद करने की सभी प्रक्रियाएँ इस उदाहरण में दिखाई गई हैं।

Fortran में File Operations का उपयोग फाइल से डेटा पढ़ने और लिखने के लिए किया जाता है। फाइल को सही तरीके से खोलना, पढ़ना, और लिखना आवश्यक होता है ताकि प्रोग्राम सुचारू रूप से चले। Fortran में फाइल को संभालने के लिए open, read, write, और close स्टेटमेंट्स का उपयोग करके आप फाइल हैंडलिंग के जटिल कार्यों को आसानी से कर सकते हैं।

Error Handling

Fortran में Error Handling का उपयोग उन समस्याओं को पहचानने और संभालने के लिए किया जाता है जो फाइल ऑपरेशन्स या अन्य प्रक्रियाओं के दौरान उत्पन्न हो सकती हैं। Error Handling का सही ढंग से उपयोग प्रोग्राम को अधिक सुरक्षित और विश्वसनीय बनाता है, क्योंकि यह सुनिश्चित करता है कि जब कोई गलती हो, तो प्रोग्राम दुर्घटनाग्रस्त न हो और उसे सही तरीके से संभाला जाए।

Error Handling के लिए IOSTAT और ERR Keyword:

Fortran में फाइल हैंडलिंग के दौरान त्रुटियों (errors) को पकड़ने के लिए IOSTAT और ERR कीवर्ड का उपयोग किया जाता है। ये कीवर्ड आपको त्रुटियों को संभालने और उनके अनुसार कार्रवाई करने की अनुमति देते हैं।

  1. IOSTAT: यह कीवर्ड किसी त्रुटि की स्थिति को ट्रैक करता है। अगर कोई त्रुटि होती है, तो IOSTAT वेरिएबल एक गैर-शून्य मान (non-zero value) लौटाता है।
  2. ERR: यह कीवर्ड एक त्रुटि होने पर प्रोग्राम को एक विशिष्ट लेबल (label) पर भेजता है, जहाँ आप त्रुटियों को संभाल सकते हैं।

1. IOSTAT का उपयोग:

IOSTAT का उपयोग करके आप यह पता लगा सकते हैं कि कोई फाइल ऑपरेशन सफल रहा या उसमें कोई त्रुटि हुई है।

उदाहरण:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
program iostat_example
integer :: a, b, io_status
! फाइल खोलते समय त्रुटि की जाँच
open(unit=10, file='input_data.txt', status='old', iostat=io_status)
if (io_status /= 0) then
print *, "Error: फाइल नहीं खुल सकी।"
else
read(unit=10, *) a, b
print *, "a:", a, "b:", b
close(unit=10)
end if
end program iostat_example
program iostat_example integer :: a, b, io_status ! फाइल खोलते समय त्रुटि की जाँच open(unit=10, file='input_data.txt', status='old', iostat=io_status) if (io_status /= 0) then print *, "Error: फाइल नहीं खुल सकी।" else read(unit=10, *) a, b print *, "a:", a, "b:", b close(unit=10) end if end program iostat_example
program iostat_example
    integer :: a, b, io_status

    ! फाइल खोलते समय त्रुटि की जाँच
    open(unit=10, file='input_data.txt', status='old', iostat=io_status)

    if (io_status /= 0) then
        print *, "Error: फाइल नहीं खुल सकी।"
    else
        read(unit=10, *) a, b
        print *, "a:", a, "b:", b
        close(unit=10)
    end if
end program iostat_example

स्पष्टीकरण:

  • iostat=io_status का उपयोग करते हुए, हम यह जाँचते हैं कि फाइल खोलने में कोई त्रुटि हुई या नहीं।
  • यदि io_status 0 के अलावा कुछ और लौटाता है, तो एक त्रुटि संदेश प्रिंट किया जाता है।

2. ERR का उपयोग:

ERR कीवर्ड का उपयोग करके आप त्रुटि होने पर प्रोग्राम को किसी विशेष लेबल (label) पर भेज सकते हैं, जहाँ आप त्रुटियों को संभाल सकते हैं।

उदाहरण:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
program err_example
integer :: a, b
! फाइल खोलते समय त्रुटि को संभालने के लिए ERR का उपयोग
open(unit=10, file='input_data.txt', status='old', err=100)
read(unit=10, *) a, b
print *, "a:", a, "b:", b
close(unit=10)
! Error label
goto 200
100 print *, "Error: फाइल नहीं खुल सकी।"
stop
200 continue
end program err_example
program err_example integer :: a, b ! फाइल खोलते समय त्रुटि को संभालने के लिए ERR का उपयोग open(unit=10, file='input_data.txt', status='old', err=100) read(unit=10, *) a, b print *, "a:", a, "b:", b close(unit=10) ! Error label goto 200 100 print *, "Error: फाइल नहीं खुल सकी।" stop 200 continue end program err_example
program err_example
    integer :: a, b

    ! फाइल खोलते समय त्रुटि को संभालने के लिए ERR का उपयोग
    open(unit=10, file='input_data.txt', status='old', err=100)

    read(unit=10, *) a, b
    print *, "a:", a, "b:", b
    close(unit=10)

    ! Error label
    goto 200

100 print *, "Error: फाइल नहीं खुल सकी।"
    stop

200 continue
end program err_example

स्पष्टीकरण:

  • यदि फाइल को खोलने में कोई त्रुटि होती है, तो प्रोग्राम लेबल 100 पर जाता है और त्रुटि संदेश प्रिंट करता है।
  • ERR कीवर्ड प्रोग्राम को एक सुरक्षित तरीके से त्रुटि को संभालने की अनुमति देता है।

3. IOMSG का उपयोग:

IOMSG कीवर्ड का उपयोग त्रुटि के बारे में एक संदेश प्राप्त करने के लिए किया जाता है। यह उपयोगी होता है जब आपको यह समझना हो कि त्रुटि किस कारण से हुई।

Syntax:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
open(unit=unit_number, file='file_name', iostat=io_status, iomsg=error_message)
open(unit=unit_number, file='file_name', iostat=io_status, iomsg=error_message)
open(unit=unit_number, file='file_name', iostat=io_status, iomsg=error_message)
  • iomsg: इसमें त्रुटि का वर्णन करने वाला एक संदेश स्टोर होता है।

उदाहरण:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
program iomsg_example
integer :: io_status
character(len=100) :: error_message
open(unit=10, file='missing_file.txt', status='old', iostat=io_status, iomsg=error_message)
if (io_status /= 0) then
print *, "Error:", trim(error_message)
else
print *, "फाइल सफलतापूर्वक खुल गई।"
close(unit=10)
end if
end program iomsg_example
program iomsg_example integer :: io_status character(len=100) :: error_message open(unit=10, file='missing_file.txt', status='old', iostat=io_status, iomsg=error_message) if (io_status /= 0) then print *, "Error:", trim(error_message) else print *, "फाइल सफलतापूर्वक खुल गई।" close(unit=10) end if end program iomsg_example
program iomsg_example
    integer :: io_status
    character(len=100) :: error_message

    open(unit=10, file='missing_file.txt', status='old', iostat=io_status, iomsg=error_message)

    if (io_status /= 0) then
        print *, "Error:", trim(error_message)
    else
        print *, "फाइल सफलतापूर्वक खुल गई।"
        close(unit=10)
    end if
end program iomsg_example

स्पष्टीकरण:

  • यदि फाइल नहीं खुलती, तो iomsg में एक त्रुटि संदेश स्टोर होता है, जिसे प्रिंट किया जाता है।

4. फाइल की स्थिति की जाँच करना (INQUIRE Statement):

Fortran में आप INQUIRE स्टेटमेंट का उपयोग करके यह जाँच सकते हैं कि कोई फाइल मौजूद है या नहीं, और उसकी विशेषताएँ क्या हैं।

Syntax:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
inquire(file='file_name', exist=file_exists)
inquire(file='file_name', exist=file_exists)
inquire(file='file_name', exist=file_exists)
  • file_exists: यह वेरिएबल .true. या .false. का मान देता है, जिससे यह पता चलता है कि फाइल मौजूद है या नहीं।

उदाहरण:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
program inquire_example
logical :: file_exists
inquire(file='input_data.txt', exist=file_exists)
if (file_exists) then
print *, "फाइल मौजूद है।"
else
print *, "Error: फाइल मौजूद नहीं है।"
end if
end program inquire_example
program inquire_example logical :: file_exists inquire(file='input_data.txt', exist=file_exists) if (file_exists) then print *, "फाइल मौजूद है।" else print *, "Error: फाइल मौजूद नहीं है।" end if end program inquire_example
program inquire_example
    logical :: file_exists

    inquire(file='input_data.txt', exist=file_exists)

    if (file_exists) then
        print *, "फाइल मौजूद है।"
    else
        print *, "Error: फाइल मौजूद नहीं है।"
    end if
end program inquire_example

स्पष्टीकरण: इस प्रोग्राम में, inquire स्टेटमेंट का उपयोग करके यह जाँच की जाती है कि input_data.txt नाम की फाइल मौजूद है या नहीं। अगर फाइल नहीं है, तो एक त्रुटि संदेश प्रिंट किया जाता है।

Error Handling Fortran में प्रोग्राम को अधिक सुरक्षित और त्रुटि-रहित बनाने के लिए आवश्यक है। IOSTAT, ERR, और IOMSG कीवर्ड्स के साथ आप फाइल ऑपरेशन्स के दौरान होने वाली त्रुटियों को पहचान सकते हैं और उन्हें सुरक्षित रूप से संभाल सकते हैं। यह तकनीकें आपको फाइल हैंडलिंग की जटिलताओं को प्रभावी ढंग से प्रबंधित करने में मदद करती हैं, जिससे आपका प्रोग्राम अधिक विश्वसनीय और मजबूत बनता है।



Index