Friday 13 December 2013

ASCII text file and Binary File - Part I

File handling शिकतांना तुम्हाला सुरवातीला ASCII Text file आणी Binary File असे काहीतरी शब्द कानावर पडतात. व अनेक विद्यार्थ्यांना नेमका अर्थ काय तेच कळत नाही. कारण विद्यार्थ्याला तो ASCII text file वर काम करतोय कि Binary या मध्येच गोंधळ होतो. खरं तर आपण दोनही वर काम केलेले असते. C/C++/Perl/HTML या सर्व कोड मध्ये आपण text file वरच काम करतो. 


ज्या File मध्ये ASCII characters असतात ती ASCII text file अशी simple definition देता येइल. Notepad किंवा vi editor वापरून आपण जी तयार करतो त्या ASCII text files च असतात. ASCII मधील A हा American असल्यामुळे ASCII text file american-centric च आहे आणी जगभर America देशाचाच Software मध्ये प्रभाव असल्याने अर्थातच ते international standard होउन बसले आहे...!

Computer Scientist हे सर्वात जास्त महत्व कशाला देत असतील तर ते Abstraction ला. हिच philosophy वापरून खरं तर OOP methodology तयार झाली. Text editor हे abstraction चे उत्तम उदाहरण म्हणता येइल. 

Memory मग ती free memory असो अथवा hard disk असो, त्या वरील डेटा हा 0s आणी 1s मध्येच स्टोअर होतो हेच तर खरे abstraction आहे. तर मग Text file असली काय किंवा binary file असली काय, शेवटी काय फरक पडतो असा प्रश्न तुम्हाला पडेल...!

त्या साठी प्रथम file म्हणजे काय ते सांगते. C मध्ये file म्हणजे Collection of bytes stored on secondary storage device like hard disk. त्या मध्ये characters असतील, words असतील, lines असतील, paragraphs असतील अथवा Data base मधील fields आणी records असतील. किंवा Image मधील pixels असतील. 

ASCII Text Files:
  1. हि एक प्रकारची Special Binary file च असते. ASCII हा 7 byte code असलेले character असते कारण एकूण ASCII characters 128 आहेत. त्यामुळे प्रत्येक कॅरेक्टर स्टोअर होतांना त्याचा most significant bit 0 (zero) असतो व तो वापरला जात नाही. Binary file च्या बाबतीत मात्र तशी काही गैरसोय होत नाहॊ व पुर्ण 256 bit patterns वापरले जातात. 
  2. शिवाय ASCII text file हि कायम Sequential स्वरूपात access करता येते. 
  3. या प्रकारची file forward direction नेच access करता येते. 
  4. याच कारणामुळे Text file हि read mode, write mode or append mode या पैकी एकाच mode मध्ये open करता येते. 
  5. ज्यावेळी data store (write) होतो त्यावेळी Newline character in text mode is converted into the carriage return - line feed combination व तशाच प्रकारची process उलट पद्धतीने होते ज्यावेळी file (read) hard disk वरून आणली जाते. अशाच प्रकारची इतर translation सुद्धा programmer च्या अपरोक्ष (background) होत असतात.
  6. फक्त कॅरेक्टर्स handle करण्याच्या पद्धतीमुळे एकावेळी एकच कॅरेक्टर read किंवा write करता येते.    

समजा एखाद्या नोटपॅड मध्ये तुम्ही cat असा word type केला व file save केली तर hard disk वर c, a, t या प्रत्येक alphabet हे corresponding ASCII value च्या स्वरूपात स्टोअर होइल. 




Binary files:
Binary files सुद्धा collection of bytes च असते. C language मध्ये Character आणी Byte एकच represent करत असल्यामुळे Binary files सुद्धा character streams म्हणूनच ओळखल्या जातात. थोडा-फार फरक आहे तो असा कि:

  1. Write किंवा Read operation च्या वेळी डेटाचे special processing होत नाही व गरज सुद्धा नसते. 
  2. C Programmer ला bit level ला काम करता येते. 
  3. Binary files sequentially आणी Randomly प्रोसेस करता येतात. 
  4. Read आणी Write operations हि एकाच वेळी (Simultaneously) करता येतात. 

ज्यावेळी तुम्ही एखादे कॅरेक्टर टाइप केल्यानंतर save होतांना तो entire byte rules प्रमाणे स्टोअर होइल.  
Binary files बरोबर सुद्धा आपण अनेकवेळा काम करतो...उदा. executable file, object files, sound files, image files या सर्व binary files category मध्ये मोडतात. उदा.

Data base files मध्ये file तयार करणे व process करणे Binary files च्या बाबतीत सोपे असते. Record update करायची असेल तर record locate करणे, मेमरी मध्ये fetch करणे, modify करून file मध्ये हव्या त्या ठिकाणी ठेवण्याची कामे सहज शक्य असतात. अशा प्रकारच्या कामाला text file शक्यतो वापरता येत नाही. 

Text आणी Binary files मध्ये broadly फरक केला जातो तो त्यांच्या 
  • new line handle करण्याच्या पद्धतीमुळे  
  • end of line represent करण्याच्या पद्धतीमुळे 
  • number store करण्याच्या पद्धतीमुळे
याच्या पुढील पोस्ट मध्ये मी एक प्रोग्रॅम घेउन सविस्तर सांगते...

Till then Happy Coding...! 

       
            

No comments:

Post a Comment

Do you like the C Marathi e-learning concept?