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

6 633 2
Làm việc với tệp

Đang tải... (xem toàn văn)

Thông tin tài liệu

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

11LẬP TRÌNH C++LẬP TRÌNH C++§12. §12. Làm việc với tệpLàm việc với tệp 22I. Các lệnh làm việc với tệpI. 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, outtrong đó mode có thể là in, app, outios::in ios::in mở tệp để đọcmở tệp để đọc 33ios::app : mở tệp để thêm vào cuốiios::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óaios::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 để đọcinFile.open(“sale.dat”, ios::in): mở tệp để đọc inFile.open(“sale.dat”) : mở tệp để đọcinFile.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óaliệu sẽ bị xóaoutFile.open(“sale.dat”, ios::app) mở tệp để thêm vào cuốioutFile.open(“sale.dat”, ios::app) mở tệp để thêm vào cuốiinFile.close() : đóng tệpinFile.close() : đóng tệpinFile.eof() = true nếu cuối tệpinFile.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 44II. Bài tập :II. Bài tập :11. Nhập dữ liệu vào tệp :. Nhập dữ liệu vào tệp :22. Đọc tệp và in ra màn hình. Đọc tệp và in ra màn hình 55#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;}} 66#include <iostream>#include <iostream>#include <fstream>#include <fstream>#include <string>#include <string>using namespace std;using namespace std;void main()void main(){{ifstream inFile;ifstream inFile;inFile.open("sale.dat",ios::in);inFile.open("sale.dat",ios::in);if (inFile.is_open())if (inFile.is_open()){{string name=""; string name=""; int sales=0;int sales=0;getline(inFile,name,'#');getline(inFile,name,'#');while (!inFile.eof())while (!inFile.eof()){{inFile>>sales;inFile>>sales;inFile.ignore(1);inFile.ignore(1);cout<<name<<'#'<<sales<<endl;cout<<name<<'#'<<sales<<endl;getline(inFile,name,'#'); getline(inFile,name,'#'); }}inFile.close(); }inFile.close(); }else cout<<"File could not be opened."<<endl;else cout<<"File could not be opened."<<endl;}} . TRÌNH C++LẬP TRÌNH C++§12. §12. Làm việc với tệpLàm việc với tệp 22I. Các lệnh làm việc với tệpI. Các lệnh làm việc với tệp Sử dụng thư viện #include. ios::in mở tệp để đọcmở tệp để đọc 33ios::app : mở tệp để thêm vào cuốiios::app : mở tệp để thêm vào cuốiios::out: mở tệp để ghi vào, nếu tệp đã có

Ngày đăng: 05/11/2012, 11:46

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan