1 / 22

آشنایی با ساختارها و کار با فایلها

آشنایی با ساختارها و کار با فایلها. جلسه دهم. فایلها. فايل: براي انتقال خروجي برنامه‌ها به حافظة پايدار بدليل ماندگاري آنها ايجاد ارتباط بين برنامه‌ها (فايل بعنوان ورودي) سلسله مراتب داده ها : بیت بایت فیلد رکورد فایل پایگاه داده نرم افزار مدیریت پایگاه داده. انواع فايلها.

reidar
Télécharger la présentation

آشنایی با ساختارها و کار با فایلها

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. آشنایی با ساختارها و کار با فایلها جلسه دهم

  2. فایلها • فايل: براي انتقال خروجي برنامه‌ها به حافظة پايدار بدليل ماندگاري آنها ايجاد ارتباط بين برنامه‌ها (فايل بعنوان ورودي) • سلسله مراتب داده ها : • بیت • بایت • فیلد • رکورد • فایل • پایگاه داده • نرم افزار مدیریت پایگاه داده

  3. انواع فايلها متن ( Text ) باينري ( Binary )

  4. ساختار اطلاعات در فایل متنی رشته‌اي از کاراکترها: براي مثال عدد 253 بصورت سه کاراکتر 2، 5 و 3 ذخيره شده و سه بايت را اشغال مي‌کند. هر سطر به کاراکترهاي پايان سطر ختم (CR/LF) ختم مي‌شود. پايان فايل را کاراکتري با کد اسکي 26 مشخص مي‌کند.

  5. مثال Adgvvh12<CR/LF>1255346asd6633<CR/LF><\26>

  6. ساختار اطلاعات در فايل باينري داده‌ها به همان شکل موجود در حافظه ذخيره مي‌شوند. براي مثال عدد 253 چون يک عدد صحيح است در 2 بايت ذخيره مي‌شود. پايان سطر در فايل باينري مفهومي ندارد. پايان فايل از طول آن مشخص مي‌شود (ديگر کاراکتر 26 پايان دهنده نيست)

  7. مثال ♣:↓☻↨•¶9◘âèÿ‡A€Ө£¥:↓☻↨ âèÿ‡A€Ө£¥ داده‌هايي با نمايش قابل فهم نيستند

  8. امكانات منطقي لازم دراستفاده از فايلها • تعريف متغير از نوع فايل • تعيين محل و عنوان فيزيکی فايل • باز کردن فايل • خواندن داده از فايل ورودی • نوشتن داده در فايل خروجی • اضافه کردن داده به انتهاي فايل • آزمون انتهای فايل درهنگام خواندن • آزمون انتهای سطر درهنگام خواندن (فقط فايل متن) • بستن فايل

  9. تعريف متغير از نوع فايل ofstream file; fstreamabc; ifstreamabc;

  10. تعيين محل و عنوان فيزيکی فايل ofstream file(“product.dat”); fstreamabc; abc.open(“xyz.dat”,ios::io);

  11. باز کردن فايل ofstream file(“product.dat”); fstreamabc; abc.open(“xyz.dat”,ios::binary|ios::io|ios::out);

  12. مدهاي فايل و معني آنها • پارامتر معني • ios::app - Append to end of file. • ios::ate - Go to end of file on opening • ios::binary - Binary file • ios::in - open file for reading only • ios::nocreate - open fails if the file does not exist • ios::noreplace - open fails if the file already exists • ios::out - open file for writing only • ios::trunc - delete the contents of the file if it exists

  13. خواندن داده از فايل ورودی obj.get(ch); obj.getline(); obj>>str; obj.read();

  14. نوشتن داده در فايل خروجی • obj.write((char*)&var, sizeof(var)); • char str[]=“Hello World \n”; • obj<<str;

  15. Simple program // basic file operations #include <iostream> #include <fstream> using namespace std; void main () { ofstreammyfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); }

  16. اضافه کردن داده به انتهاي فايل • با استفاده از جريان فايل (file stream): • در هنگام تعريف و يا بازكردن فايل مد آن بصورت زير باشد. ios::app - Append to end of file. fstream abc; abc.open(“xyz.dat”,ios::io|ios::app);

  17. آزمون انتهای فايل درهنگام خواندن • با استفاده از جريان فايل (file stream): fstream ioFile; if ( !ioFile ) { cout<<“End of File”; ioFile.close() ;} while(! ioFile.eof())

  18. بستن فايل • Fout.close();

  19. FUNCTIONS FOR MANIPULATION OF FILE POINTERS: • The file stream classes support the following functions to move a file pointer to any other desired position inside the file. • seekg(): Moves get pointer (input) to a specified location. • seekp(): Moves put pointer (output) to a specified location. • tellg(): Gives the current position of the get pointer. • tellp(): Gives the current position of the put pointer. • The seekg & tellg functions are associated with get pointer and seekp & tellp functions are associated with put pointer.

  20. NOTES REGARDING SEEK FUNCTIONS: • seekg() and tellg() can also be used with two arguments as follwos: • seekg(offset, refposition): • seekp(offset, refposition); • The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. The refposition takes one of the following three constants defined in the ios class: • ios::beg start of the file • ios::cur current position of the pointer • ios::end end of the file • The seekg() function moves the associated file’s get pointer while the seekp() function moves the associated file’s put pointer.

  21. POINTER OFFSET CALLS : Seek call Action fout.seekg(0,ios::beg); -go to start fout.seekg(0,ios::cur); -stay at the current position fout.seekg(0,ios::end); -go to end of file fout.seekg(m,ios::beg); -move to (m+1)th byte in the file. fout.seekg(m,ios::cur); -go forward by m byte from the current position fout.seekg(+m,ios::cur); -go forward by m bytes from the current position fout.seekg(-m,ios::cur); -go backward by m bytes from the end

  22. Simple program // obtaining file size #include <iostream> #include <fstream> using namespace std; void main () { long begin,end; ifstreammyfile ("example.txt"); begin = myfile.tellg(); myfile.seekg (0, ios::end); end = myfile.tellg(); myfile.close(); cout << "size is: " << (end-begin) << " bytes.\n"; }

More Related