1 / 14

Chương 6: Stream và File

Chương 6: Stream và File. Nội dung chính. C++ File I/O File nhị phân Con trỏ File. 1. C++ File I/O. I/O trong được xây dựng trên 3 class: istream, ostream và iostream. C++ coi các file là luồng các byte (stream).

Télécharger la présentation

Chương 6: Stream và File

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chương 6: Stream và File

  2. Nội dung chính • C++ File I/O • File nhị phân • Con trỏ File

  3. 1. C++ File I/O • I/O trong được xây dựng trên 3 class: istream, ostream và iostream. • C++ coi các file là luồng các byte (stream). • Khi I/O với các file trên đĩa, ta phải sử dụng các lớp ifstream, ofstream, và fstream được định nghĩa trong <fstream.h>. • Chú ý: • ifstream thừa kế từ istream • ofstream thừa kế từ ostream • fstream thừa kế từ iostream

  4. Input File • Để đọc 100 số từ file numbers.dat, phải khai báo biến có kiểu input file. ifstream data_file; • Xác định tên file cần đọc, bằng cách sử dụng hàm open. data_file.open("numbers.dat" ); • Đọc dữ liệu từ file ra: for (i = 0; i < 100; ++i) data_file >> data_array[i]; • Sau khi sử dụng xong, đóng file lại. data_file.close();

  5. Input File … • C++ cho phép gọi open ngay trong hàm tạo: ifstream data_file("numbers.dat"); • Khi mở file sử dụng chồng toán tử ! để kiểm tra file có được mở thành công hay không • Khi đọc file, để kiểm tra lỗi sử dụng hàm bad. if (data_file.bad()) { cerr << "Unable to open numbers.dat\n"; exit (8); } • Hàm getline cho phép đọc cả dòng dữ liệu từ file: istream &getline(char *buffer, int len, char delim = '\n') • buffer Là bộ đệm lưu dữ liệu • len là chiều dài của bộ đệm. • Chú ý, ký tự kết thúc không được lưu trong bộ đệm.

  6. Output file • Các hàm dùng cho input file và output file tương tự nhau. ofstream out_file("out.dat"); • Định nghĩa đầy đủ: ofstream::ofstream(const char *name, int mode=ios::out, int prot = filebuf::openprot); • name là tên file • mode là kiể mở file • ios: :app ghi vào cuối file • ios: :ate khi file đang mở, nhảy xuống cuối file • ios::in mở để nhập vào(chỉ áp dụng cho opens của biến ifstream). • ios: :out mở để hiển thị(chỉ áp dụng cho opens của biến ofstream). • ios: :binary Binary file • ios:: trunc Loại bỏ nội dung của file đang tồn tại khi mở file để ghi. • ios::nocreate Lỗi nếu file không tồn tại.(Chỉ áp dụng cho output file, input file luôn lỗi nếu không tồn tại file). • ios::noreplace Không ghi đè lên file đã tồn tại. • prot:Bảo vệ file. Giá trị này phụ thuộc vào OS. • UNIX 0644 • Windows là 0.

  7. Output File … • Ví dụ: ofstream out_file("data.new",ios::out|ios::binary|ios::nocreate|ios::app); • Ghi tiếp dữ liệu nhị phân vào file đã tồn tại, có tên là “data.new”

  8. 2. File nhị phân • Vào ra nhị phân được sử dụng với 2 hàm thành viên: read và write. in_file.read(dataptr, size); out_file.write(data_ptr, size); • data_ptr: con trỏ trỏ tới vị trí đặt dữ liệu • size: số lượng byte cần đọc • Hàm gcount: trả về số byte được lấy ra từ lần đọc cuối cùng. (có thể nhỏ hơn số byte yêu cầu). • Hàm eof: kiểm tra kết thúc file

  9. Ví dụ struct { int width; int height; } rectangle; in_file.read((char *)(&rectangle), sizeof(rectangle)); if (infile.bad()) { cerr << "Unable to read rectangle\n"; exit (8); } if (in_file.gcount() != sizeof(rectangle)) { cerr << "Error: Unable to read full rectangle\n"; cerr << "I/O error of EOF encountered\n"; } Dữ liệu được đưa vào một struct. Toán tử & thể hiện là con trỏ kiểu rectangle và được ép kiểu thành con trỏ kiểu char (char *). Toán tử sizeof được sử dụng để xác định bao nhiêu byte cần đọc.

  10. 3. Con trỏ File • Mỗi đối tượng file có hai giá trị nguyên là get pointer và put poiter - để xác định vị trí nào trong file cần đọc hoặc ghi. • Hàm seekg() và tellg() cho phép thiết lập và kiểm tra get pointer. • Hàm seekp() và tellp() cho phép thiết lập và kiểm tra put pointer.

  11. Con trỏ File … • seekg() • có 1 tham số: xác định vị trí bắt đầu file • có 2 tham số • Tham số thứ nhất xác định vị trí offset • Tham số thứ hai xác định hướng dịch chuyển • beg: từ đầu file đến vị tri offset • cur: từ vị trí hiện tại đến vị trí offset • end: từ cuối file ngược đến offset infile.seekg(0,ios::end) //di chuyển từ đầu đến cuối file

  12. Con trỏ File … • tellg(): trả về vị trí hiện tại của get point

  13. Ví dụ class person { protected: char name[80]; int age; public: void getdata() { cout<<"\n Enter name:"; cin>>name; cout<<"Enter age:"; cin>>age; } void showdata() { cout<<"\n Name:"<<name; cout<<"\n Age:"<<age; } };

  14. Ví dụ … int main(int argc, char* argv[]) { person p; fstream outfile("Person.txt",ios::out|ios::in| ios::binary|ios::app); for(int i=0;i<3;i++) { p.getdata(); outfile.write((char*)(&p),sizeof(p)); } outfile.seekg(0); do { outfile.read((char*)(&p),sizeof(p)); p.showdata(); }while(! outfile.eof()); return 0; }

More Related