<< 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 does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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