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

Preview:

DESCRIPTION

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

Citation preview

دهم جلسه

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

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

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

(Text) متن

(Binary) باينري

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

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

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

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

ختم مي شود.

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

مشخص مي کند.

Adgvvh12<CR/LF>1255346asd6633<CR/

LF><\26>

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

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

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

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

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

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

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

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

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

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

ofstream file;

fstream abc;

ifstream abc;

ofstream file(“product.dat”);

fstream abc;

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

ofstream file(“product.dat”);

fstream abc;

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

پارامتر معني 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

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

obj>>str;obj.read;()

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

// 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(); }

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

ios::app - Append to end of file.

fstream abc;

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

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

if ( !ioFile ) {

cout<<“End of File”;

ioFile.close() ;}

while(! ioFile.eof())

Fout.close;()

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.

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.

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

// 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";}