Tải bản đầy đủ (.ppt) (6 trang)

Làm việc với tệp

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (473.16 KB, 6 trang )





1
1
LẬP TRÌNH C++
LẬP TRÌNH C++
§12.
§12.
Làm việc với tệp
Làm việc với tệp




2
2
I. Các lệnh làm việc với tệp
I. Các lệnh làm việc với tệp

Sử dụng thư viện #include <fstream>
Sử dụng thư viện #include <fstream>

ifstream ten_bien_tep_doc;
ifstream ten_bien_tep_doc;

ofstream ten_ bien_tep_ghi;
ofstream ten_ bien_tep_ghi;

Ví dụ :


Ví dụ :

ifstream inFile;
ifstream inFile;

ofstream outFile;
ofstream outFile;

Để mở tệp ta dùng lệnh :
Để mở tệp ta dùng lệnh :
ten_bien_tep.open(“ten_tep”,ios::mode);
ten_bien_tep.open(“ten_tep”,ios::mode);

trong đó mode có thể là in, app, out
trong đó mode có thể là in, app, out

ios::in
ios::in
mở tệp để đọc
mở tệp để đọc




3
3

ios::app : mở tệp để thêm vào cuối
ios::app : mở tệp để thêm vào cuối


ios::out: mở tệp để ghi vào, nếu tệp đã có thì dữ liệu sẽ bị xóa
ios::out: mở tệp để ghi vào, nếu tệp đã có thì dữ liệu sẽ bị xóa

Ví dụ :
Ví dụ :

inFile.open(“sale.dat”, ios::in): mở tệp để đọc
inFile.open(“sale.dat”, ios::in): mở tệp để đọc



inFile.open(“sale.dat”) : mở tệp để đọc
inFile.open(“sale.dat”) : mở tệp để đọc

outFile.open(“sale.dat”, ios::out) : mở tệp để ghi vào, nếu tệp
outFile.open(“sale.dat”, ios::out) : mở tệp để ghi vào, nếu tệp
đã có thì dữ liệu sẽ bị xóa
đã có thì dữ liệu sẽ bị xóa

outFile.open(“sale.dat”) mở tệp để ghi vào, nếu tệp đã có thì dữ
outFile.open(“sale.dat”) mở tệp để ghi vào, nếu tệp đã có thì dữ
liệu sẽ bị xóa
liệu sẽ bị xóa

outFile.open(“sale.dat”, ios::app) mở tệp để thêm vào cuối
outFile.open(“sale.dat”, ios::app) mở tệp để thêm vào cuối

inFile.close() : đóng tệp
inFile.close() : đóng tệp


inFile.eof() = true nếu cuối tệp
inFile.eof() = true nếu cuối tệp

cin.ignore(1); : bỏ qua ký tự con sót lại
cin.ignore(1); : bỏ qua ký tự con sót lại




4
4
II. Bài tập :
II. Bài tập :
1
1
. Nhập dữ liệu vào tệp :
. Nhập dữ liệu vào tệp :
2
2
. Đọc tệp và in ra màn hình
. Đọc tệp và in ra màn hình




5
5
#include <iostream>
#include <iostream>
#include <fstream>

#include <fstream>
#include <string>
#include <string>
using namespace std;
using namespace std;
void main()
void main()
{
{
ofstream outFile;
ofstream outFile;
outFile.open("sale.dat",ios::out);
outFile.open("sale.dat",ios::out);
if (outFile.is_open())
if (outFile.is_open())
{
{
string name=""; int sales=0;
string name=""; int sales=0;
cout<<"Name (X to stop): "; getline(cin,name);
cout<<"Name (X to stop): "; getline(cin,name);
while (name!="X" && name!="x")
while (name!="X" && name!="x")
{
{
cout<<"Sales: "; cin>>sales;
cout<<"Sales: "; cin>>sales;
cin.ignore(1);
cin.ignore(1);
outFile<<name<<'#'<<sales<<endl;

outFile<<name<<'#'<<sales<<endl;
cout<<"Name (X to stop): "; getline(cin,name); }
cout<<"Name (X to stop): "; getline(cin,name); }
outFile.close();}
outFile.close();}
else cout<<"File could not be opened."<<endl;
else cout<<"File could not be opened."<<endl;
}
}

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×