<< Chapter < Page Chapter >> Page >

Nhập/xuất không định dạng với read(),gcount() và write()

Nhập/xuất không định dạng được thực hiện với các hàm thành viên istream::read() và ostream::write().

Hàm istream::read():

istream&read(unsigned char* puch, int nCount);

istream&read(signed char* psch, int nCount);

Trích các byte từ dòng cho đến khi giới hạn nCount đạt đến hoặc cho đến khi end- of-file đạt đến. Hàm này có ích cho dòng nhập nhị phân.

Hàm ostream::write():

ostream&write(const unsigned char* puch, int nCount);

ostream&write(const signed char* psch, int nCount);

Chèn nCount byte vào từ vùng đệm (được trỏ bởi puch và psch) vào dòng. Nếu file được mở ở chế độ text, các ký tự CR có thể được chèn vào. Hàm này có ích cho dòng xuất nhị phân. Chẳng hạn:

char Buff[]="HAPPY BIRTHDAY";

cout.write(Buff,10);

Hàm istream::gcount():

int gcount();

Hàm trả về số ký tự đã trích bởi hàm nhập không định dạng cuối cùng.

Dòng nhập/ xuất file

Để thực thi xử lý file trong C++, các chương trình phải include tập tin<iostream.h>và<fstream.h>. Header<fstream.h>gồm định nghĩa cho các lớp dòng ifstream cho nhập (đọc) từ một file, ofstream cho xuất (ghi) tới một file) và fstream cho nhập/xuất (đọc/ghi) tới một file. Các file được mở bằng cách tạo các đối tượng của các lớp dòng này. Cây phả hệ của các lớp này ở hình 8.2.

 Constructor của lớp ofstream:

(1) ofstream();

(2) ofstream(const char* szName,int nMode=ios::out,int nProt=filebuf::openprot);

(3) ofstream(int fd);

(4) ofstream(filedesc fd, char* pch, int nLength);

Trong đó:szName: Tên file được mở.

nMode: Một số nguyên chứa các bit mode định nghĩa là kiểu liệy kê của ios. Có thể kết hợp bằng toán tử |. Tham số này có thể một trong các giá trị sau:

Mode Ý nghĩa
ios::app Hàm di chuyển con trỏ file tới end-of-file. Khi các byte mới được ghi lên file, chúng luôn luôn nối thêm vào cuối, ngay cả vị trí được di chuyển với hàm ostream::seekp().
ios::ate Hàm di chuyển con trỏ file tới end-of-file. Khi byte mới đầu tiên được ghi lên file, chúng luôn luôn nối thêm vào cuối, nhưng khi các byte kế tiếp được ghi, chúng ghi vào vị trí hiện hành.
ios::in Mở file để đọc.Với dòng ifstream, việc mở file đương nhiên được thực hiện ở chế độ này.
ios::out Mở file để đọc.Với dòng ofstream, việc mở file đương nhiên được thực hiện ở chế độ này.
ios::trunc Xóa file hiện có trên đĩa và tạo file mới cùng tên. Cũng có hiểu đây là chặt cụt file cũ, làm cho kích thước của nó bằng 0, chuẩn bị ghi nội dung mới. Mode này được áp dụng nếu ios::out được chỉ định và ios::app, ios::ate, và ios::in không được chỉ định.
ios::nocreate Nếu file không tồn tại thì thao tác mở thất bại.
ios::noreplace Nếu file tồn tại thì thao tác mở thất bại.
ios::binary Mở file ở chế độ nhị phân (mặc định là ở chế độ văn bản).

nProt: Đặc tả chế độ bảo vệ file.

fd: Mã nhận diện file.

pch: Con trỏ trỏ tới vùng dành riêng chiều dài nLength. Giá trị NULL (hoặc nLength=0) dẫn đến dòng không vùng đệm.

nLength: Chiều dài tính theo byte của vùng dành riêng (0=không vùng đệm).

Dạng (1) xây dựng một đối tượng ofstream mà không mở file.

Dạng (2) xây dựng một đối tượng ofstream và mở file đã chỉ định.

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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, 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