c++ - What's the difference between read() and getc() -
i have 2 code segments:
while((n=read(0,buf,buffsize))>0) if(write(1,buf,n)!=n) err_sys("write error"); while((c=getc(stdin))!=eof) if(putc(c,stdout)==eof) err_sys("write error");
some sayings on internet make me confused. know standard i/o buffering automatically, have passed buf read()
, read()
doing buffering, right? , seems getc()
read data char char, how data buffer have before sending data out?
thanks
while both functions can used read file, different. first of on many systems read
lower-level function, , may system call directly os. read
function isn't standard c or c++, it's part of e.g. posix. can read arbitrarily sized blocks, not 1 byte @ time. there's no buffering (except maybe @ os/kernel level), , doesn't differ between "binary" , "text" data. , on posix systems, read
system call, can used read kind of devices , not files.
the getc
function higher level function. uses buffered input (so input read in blocks buffer, using read
, , getc
function gets characters buffer). returns single characters @ time. it's part of c , c++ specifications part of standard library. also, there may conversions of data read , data returned function, depending on if file opened in text or binary mode.
another difference read
function, while getc
might preprocessor macro.
comparing read
, getc
doesn't make sense, more sense comparing read
fread
.
Comments
Post a Comment