کار با فايلها(جديد)

Download Report

Transcript کار با فايلها(جديد)

‫جلسه دهم‬
‫‪‬‬
‫‪‬‬
‫فايل‪ :‬براي انتقال خروجي برنامهها به حافظة پايدار بدليل ماندگاري آنها‬
‫ايجاد ارتباط بين برنامهها (فايل بعنوان ورودي)‬
‫سلسله مراتب داده ها ‪:‬‬
‫◦‬
‫◦‬
‫◦‬
‫◦‬
‫◦‬
‫◦‬
‫◦‬
‫بيت‬
‫بايت‬
‫فيلد‬
‫رکورد‬
‫فايل‬
‫پايگاه داده‬
‫نرم افزار مديريت پايگاه داده‬
( Text ) ‫متن‬
( Binary ) ‫باينري‬
‫رشتهاي از کاراکترها‪ :‬براي مثال عدد ‪253‬‬
‫بصورت سه کاراکتر ‪ 5 ،2‬و ‪ 3‬ذخيره شده و سه‬
‫بايت را اشغال ميکند‪.‬‬
‫هر سطر به کاراکترهاي پايان سطر ختم )‪(CR/LF‬‬
‫ختم ميشود‪.‬‬
‫پايان فايل را کاراکتري با کد اسکي ‪ 26‬مشخص‬
Adgvvh12<CR/LF>1255346asd
6633<CR/LF><\26>
‫دادهها به همان شکل موجود در حافظه ذخيره‬
‫ميشوند‪.‬‬
‫براي مثال عدد ‪ 253‬چون يک عدد صحيح است در ‪ 2‬بايت ذخيره ميشود‪.‬‬
‫پايان سطر در فايل باينري مفهومي ندارد‪.‬‬
‫پايان فايل از طول آن مشخص ميشود‬
‫(ديگر کاراکتر ‪ 26‬پايان دهنده نيست)‬
‫♣‪↨☻↓:âèÿ‡A€Ө£¥◘9¶•↨☻↓:‬‬
‫‪âèÿ‡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();
}
ios::app
:)file stream( ‫با استفاده از جريان فايل‬
.‫در هنگام تعريف و يا بازكردن فايل مد آن بصورت زير باشد‬
Append to end of file.
fstream abc;
abc.open(“xyz.dat”,ios::io|ios::app);


fstream ioFile;
if ( !ioFile ) {
cout<<“End of File”;
ioFile.close() ;}
while(! ioFile.eof())
:)file stream( ‫با استفاده از جريان فايل‬

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::beg
start of the file
(ii) ios::cur
current position of the pointer
(iii) 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.
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";
}