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

22
م ه ه د س ل ج

Upload: reidar

Post on 05-Jan-2016

68 views

Category:

Documents


7 download

DESCRIPTION

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

TRANSCRIPT

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

دهم جلسه

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

فايل: براي انتقال خروجي برنامه ها به حافظةپايدار بدليل ماندگاري آنها ايجاد ارتباط بين

برنامه ها )فايل بعنوان ورودي(: سلسله مراتب داده ها

بیت◦بایت◦فیلد◦رکورد◦فایل◦پایگاه داده◦نرم افزار مدیریت پایگاه داده◦

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

(Text) متن

(Binary) باينري

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

253رشته اي از کاراکترها: براي مثال عدد

ذخيره شده و سه 3 و 5، 2بصورت سه کاراکتر

بايت را اشغال مي کند.

(CR/LF)هر سطر به کاراکترهاي پايان سطر ختم

ختم مي شود.

26پايان فايل را کاراکتري با کد اسکي

مشخص مي کند.

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

Adgvvh12<CR/LF>1255346asd6633<CR/

LF><\26>

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

داده ها به همان شکل موجود در

حافظه ذخيره مي شوند. 2 چون يک عدد صحيح است در 253براي مثال عدد

بايت ذخيره مي شود.

پايان سطر در فايل باينري مفهومي ندارد.

پايان فايل از طول آن مشخص مي شود

پايان دهنده نيست(26)ديگر کاراکتر

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

¶•↨☻↓:♣9◘âèÿ‡A€Ө£¥:↓☻↨ âèÿ‡A€Ө¥£

داده هايي با نمايش قابل فهم نيستند

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

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

متن(بستن فايل

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

ofstream file;

fstream abc;

ifstream abc;

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

ofstream file(“product.dat”);

fstream abc;

abc.open(“xyz.dat”,ios::io);

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

ofstream file(“product.dat”);

fstream abc;

abc.open(“xyz.dat”,ios::binary|ios::io|ios::out);

Page 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

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

obj.get(ch);obj.getline;()

obj>>str;obj.read;()

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

obj.write((char*)&var, sizeof(var)); char str[]=“Hello World \n”; obj<<str;

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

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

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

) با استفاده از جريان فايلfile stream :(.در هنگام تعريف و يا بازكردن فايل مد آن بصورت زير باشد

ios::app - Append to end of file.

fstream abc;

abc.open(“xyz.dat”,ios::io|ios::app);

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

) با استفاده از جريان فايلfile stream :( fstream ioFile;

if ( !ioFile ) {

cout<<“End of File”;

ioFile.close() ;}

while(! ioFile.eof())

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

Fout.close;()

Page 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.

1. seekg(): Moves get pointer (input) to a specified location.

2. seekp(): Moves put pointer (output) to a specified location.

3. tellg(): Gives the current position of the get pointer.

4. 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.

Page 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:

(i) ios::begstart of the file

(ii) ios::cur current position of the pointer

(iii) ios::endend of the file

The seekg() function moves the associated file’s get pointer while the seekp() function moves the associated file’s put pointer.

Page 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

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

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