<< Chapter < Page Chapter >> Page >
Mục tiêu: Chương này nhằm mục đích rèn luyện sinh viên các kỹ năng thao tác với hệ thống tập tin của Windows trong VB. Bên cạnh đó, việc hệ thống lại các kiến thức của các chương trước cũng là một mục tiêu quan trọng của chương.

Học xong chương này, sinh viên phải nắm được các vấn đề sau:

- Sử dụng mô hình hệ thống tập tin.

- Cách thức truy cập tập tin tuần tự.

- Cách thức truy cập tập tin truy xuất ngẫu nhiên.

Kiến thức có liên quan:

Giáo trình Visual Basic, Chương 7.

Tài liệu tham khảo:

- Visual Basic 6 Certification Exam Guide - Chapter 7, Page 191; Chapter 13, Page 377 - Dan Mezick&Scot Hillier - McGraw-Hill - 1998.

BÀI TẬP HƯỚNG DẪN

Bài tập 3-1:

XUẤT NHẬP TẬP TIN VĂN BẢN

Bước 1: Tạo Project mới tên Bt3-1 trong thư mục Basic\Bt3-1. Tạo giao diện có dạng sau:

Hình III.1: Tập tin văn bản

Item 1 – TextBox

NameText1

Height2220

Width6630

MultilineTrue

ScrollBarsBoth

Item 2 – CommandButton

NameCommand1

CaptionNew

Item 3 – CommandButton

NameCommand2

CaptionOpen

Item 4 – CommandButton

NameCommand3

CaptionSave

Item 5 – CommandButton

NameCommand4

CaptionPrint

Item 6 – CommandButton

NameCommand5

CaptionCopy

Bước 2: Nút New có nhiệm vụ xóa văn bản trong TextBox để ta có thể khởi tạo một tài liệu mới. Do đó, trong hàm sự kiện Command1_Click, thêm vào đoạn mã:

Text1.Text = “”

Ghi chuỗi lên tập tin

Bước 3: Ở đây ta nhập vào đoạn văn bản rồi ghi lên tập tin. Để đơn giản ta đọc và ghi từ một tập tin văn bản duy nhất tên là vidu.txt nằm trong thư mục của dự án của mình (ở đây là thư mục Bt5-1). Để ghi lên tập tin, trong hàm sự kiện Command3_Click, thêm đoạn mã sau:

' Ghi len tap tin

Open App.Path&"\vidu.txt" For Output As #1

' Ghi du lieu

Print #1, Text1.Text

' Dong tap tin

Close #1

MsgBox "Van ban da duoc luu"

Bước 4: Việc thao tác trên tập tin được thực hiện nhờ thẻ tập tin. Thực chất đây là một số nguyên chỉ bởi VB một liên kết đến một tập tin xác định để xuất hay nhập vào tập tin đó. Ở đây là sử dụng #1. Câu lệnh Print sử dụng thẻ tập tin để ghi văn bản lên tập tin. Khi việc ghi hoàn tất, thẻ tập tin được đóng lại nhờ câu lệnh Close.

Bước 5: Chạy ứng dụng, nhấp nút Command3. Nếu chương trình thực thi tốt, ta có thể mở tập tin vidu.txt trong Notepad xem.

ĐỌC TỪ TẬP TIN VĂN BẢN

Bước 6: Đọc tập tin từ đĩa tương tự như ghi tập tin. Chèn đoạn mã sau trong hàm xử lý sự kiện Command2_Click

Text1.Text = ""

Close #1

' Mo tap tin

Open App.Path&"\vidu.txt" For Input As #1

Dim filetext As String ' Bien chuoi luu van ban

Do While Not EOF(1)

Input #1, filetext ' Doc tung dong

' Hien thi trong TextBox, chu y them vao ky tu xuong dong

Text1.Text = Text1.Text&filetext&vbCrLf

Loop

Close #1

Bước 7: Chạy ứng dụng. Nhấp nút Command2 để đọc từ tập tin vidu.txt vào TextBox. Ở đây ta có định nghĩa một biến trong lệnh

Dim filetext as String

Ở đây, mỗi lần ta đọc từng dòng trong tập tin vidu.txt; mỗi lần đọc như vậy ta lưu vào biến kiểu chuỗi filetext; sau đó ta nối chuỗi filetext vào sau chuỗi Text1.Text (hiển thị trong TextBox). Quá trình trên được thực hiện liên tục đến khi đọc hết nội dung tập tin nhờ vào vòng lặp:

Do While Not EOF(1)

EOF là một hàm được định nghĩa sẵn trong VB, hàm này có nhiệm vụ kiểm tra xem có đạt đến cuối tập tin hay không? Nếu nội dung tập tin vẫn chưa được đọc hết, quá trình đọc vẫn tiếp tục đến khi EOF là True.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Giáo trình visual basic. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10777/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Giáo trình visual basic' conversation and receive update notifications?

Ask