<< Chapter < Page Chapter >> Page >

4: #include<stdlib.h>

5:

6: int main()

7: {

8: ofstream OutClientFile("clients.dat", ios::out);

9: if (!OutClientFile)

10: {

11: cerr<<"File could not be opened"<<endl;

12: exit(1);

13: }

14: cout<<"Enter the Account, Name, and Balance."<<endl

15:         <<"Enter EOF to end input."<<endl<<"? ";

16: int Account;

17: char Name[10];

18: float Balance;

19: while (cin>>Account>>Name>>Balance)

20: {

21: OutClientFile<<Account<<" "<<Name

22:         <<" "<<Balance<<endl;

23: cout<<"? ";

24: }

25: OutClientFile.close();

26: return 0;

27: }

Chúng ta chạy ví dụ 8.28 , kết quả ở hình 8.30

Hình 8.30: Kết quả của ví dụ 8.28

Ví dụ 8.29: Đọc file văn bản tạo ở ví dụ 8.28 và xuất ra màn hình.

1: //Chương trình 8.29

2: #include<iostream.h>

3: #include<fstream.h>

4: #include<iomanip.h>

5: #include<stdlib.h>

6:

7: void OutputLine(int, char*, float);

8:

9: int main()

10: {

11: ifstream InClientFile("clients.dat", ios::in);

12: if (!InClientFile)

13: {

14: cerr<<"File could not be opened"<<endl;

15: exit(1);

16: }

17: int Account;

18: char Name[10];

19: float Balance;

20: cout<<setiosflags(ios::left)<<setw(10)<<"Account"

21:      <<setw(13)<<"Name"<<"Balance"<<endl;

22: while (InClientFile>>Account>>Name>>Balance)

23: OutputLine(Account, Name, Balance);

24: InClientFile.close();

25: return 0;

26: }

27:

28: void OutputLine(int Acct, char *Name, float Bal)

29: {

30: cout<<setiosflags(ios::left)<<setw(10)<<Acct

31:  <<setw(13)<<Name<<setw(7)<<etprecision(2)

32:<<setiosflags(ios::showpoint | ios::right)<<Bal<<endl;

33: }

Chúng ta chạy ví dụ 8.29 , kết quả ở hình 8.31

Hình 8.31: Kết quả của ví dụ 8.29

Do lớp ifstream dẫn xuất từ lớp istream nên chúng ta có thể dùng các hàm istream::get(), istream::getline().

Ví dụ 8.30: Đọc file văn bản tạo ở ví dụ 8.28 bằng hàm istream::getline() và xuất ra màn hình.

1: //Chương trình 8.30

2: #include<iostream.h>

3: #include<fstream.h>

4: #include<iomanip.h>

5: #include<stdlib.h>

6:

7: #define MAXLINE 256

8:

9: int main()

10: {

11: ifstream InClientFile("clients.dat", ios::in);

12: if (!InClientFile)

13: {

14: cerr<<"File could not be opened"<<endl;

15: exit(1);

16: }

17: char Line[MAXLINE];

18: while (!InClientFile.eof())

19: {

20: InClientFile.getline(Line,MAXLINE-1);

21: cout<<Line<<endl;

22: }

23: InClientFile.close();

24: return 0;

25: }

Chúng ta chạy ví dụ 8.30 , kết quả ở hình 8.32.(nội dung tập tin clients.dat )

Hình 8.32: Kết quả của ví dụ 8.30

Nhập/xuất file nhị phân

Đối với file nhị phân (còn gọi là file truy cập ngẫu nhiên), chúng ta mở ở chế độ ios::binary. Đối với file nhị phân, chúng ta có thể dùng hàm istream::get() và ostream::put() để đọc và ghi từng byte một. Để đọc và ghi nhiều byte cùng lúc chúng ta có thể dùng istream::read() và ostream::write().

Ví dụ 8.31: Lấy lại ví  dụ 8.28 nhưng lưu dữ liệu dưới dạng nhị phân.

1: //Chương trình 8.31

2: #include<iostream.h>

3: #include<fstream.h>

4: #include<stdlib.h>

5:

6: typedef struct

7: {

8: int Account;

9: char Name[10];

10: float Balance;

11: }Client;

12:

13: int main()

14: {

15: ofstream OutClientFile("credit.dat", ios::out|ios::binary);

16: if (!OutClientFile)

17: {

18: cerr<<"File could not be opened"<<endl;

19: exit(1);

20: }

21: cout<<"Enter the Account, Name, and Balance."<<endl

22:         <<"Enter EOF to end input."<<endl<<"? ";

23: Client C;

24: while (cin>>C.Account>>C.Name>>C.Balance)

25: {

26: OutClientFile.write((char *)&C,sizeof(Client));

27: cout<<"? ";

28: }

29: OutClientFile.close();

30: return 0;

31: }

Chúng ta chạy ví dụ 8.31 , kết quả ở hình 8.33 (nội dung tập tin credit.dat )

Hình 8.33: Kết quả của ví dụ 8.31

Ví dụ 8.32: Đọc file tạo ở ví dụ 8.31 và xuất ra màn hình.

1: //Chương trình 8.32

2: #include<iostream.h>

3: #include<fstream.h>

4: #include<iomanip.h>

5: #include<stdlib.h>

6:

7: typedef struct

8: {

9: int Account;

10: char Name[10];

11: float Balance;

12: }Client;

13:

14: void OutputLine(Client);

15:

16: int main()

17: {

18: ifstream InClientFile("credit.dat", ios::in|ios::binary);

19: if (!InClientFile)

20: {

21: cerr<<"File could not be opened"<<endl;

22: exit(1);

23: }

24: cout<<setiosflags(ios::left)<<setw(10)<<"Account"

25:         <<setw(13)<<"Name"<<"Balance"<<endl;

26: Client C;

27: while (InClientFile.read((char *)&C,sizeof(Client)))

28: OutputLine(C);

29: InClientFile.close();

30: return 0;

31: }

32:

33: void OutputLine(Client C)

34: {

35: cout<<setiosflags(ios::left)<<setw(10)<<C.Account

36:         <<setw(13)<<C.Name<<setw(7)<<setprecision(2)

37:         <<setiosflags(ios::showpoint | ios::right)<<C.Balance<<endl;

38: }

Chúng ta chạy ví dụ 8.32 , kết quả ở hình 8.34

Hình 8.34: Kết quả của ví dụ 8.32

Bài tập

Bài 1: Cài đặt toán tử>>và<<cho kiểu dữ liệu mảng để nhập và xuất.

Bài 2: Cài đặt thêm toán tử>>và<<cho lớp Time trong bài 5 của chương 4.

Bài 3: Cài đặt thêm toán tử>>và<<cho lớp Date trong bài 6 của chương 4.

Bài 4: Viết chương trình kiểm tra các giá trị nguyên nhập vào dạng hệ 10, hệ 8 và hệ 16.

Bài 5: Viết chương trình in bảng mã ASCII cho các ký tự có mã ASCII từ 33 đến 126. Chương trình in gồm giá trị ký tự, giá trị hệ 10, giá trị hệ 8 và giá trị hệ 16.

Bài 6: Viết chương trình in các giá trị nguyên dương luôn có dấu + ở phía trước.

Bài 7: Viết chương trình tương tự như lệnh COPY của DOS để sao chép nội dung của file .

Bài 8: Viết chương trình cho biết kích thước file.

Bài 9: Viết chương trình đếm số lượng từ có trong một file văn bản tiếng Anh.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Lập trình hướng đối tượng. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10794/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Lập trình hướng đối tượng' conversation and receive update notifications?

Ask