❶ C# 濡备綍鍦ㄧ嚎绋嬩腑鍏抽棴鏂囦欢娴侊纻
1.濡傛灉绾跨▼姝e湪浣跨敤,搴旇ュ厛鎸傝捣,鍦ㄥ叧闂,
2.鍏抽棴鏂囦欢娴佸拰鍒镄勪竴镙,閮芥槸close
3.杩欎釜链夊氱嶅彲鑳,鍙鑳戒綘镄勭▼搴忔病鍏,鍙鑳界嚎绋嫔湪杩愯,鍙鑳戒綘鐢ㄧ殑鏄鍐呭瓨缂揿啿寰鏂囦欢閲屽啓,娌℃湁Flush(); 涓涓
❷ java涓鏂囦欢镓揿紑鍜屽叧闂镄勯梾棰
JVM阃鍑烘椂浼氶喷鏀炬墍链夋枃浠剁殑鍙ユ焺锛岃繖涓鏂囦欢褰撶劧鏄琚镊锷ㄥ叧闂浜嗭绂濡傛灉浣犲湪鍐椤叆鏂囦欢钖庯纴娌℃湁鍙婃椂璋幂敤flush()锛屽啓鍏ョ殑鍐呭逛笉浼氩埛鍏ョ佺洏锛屽氨涓㈠け浜.
❸ c璇瑷涓,鎶婃枃浠剁湅浣滀竴绯诲垪镄勪粈涔堟祦
璇ヨ$畻链虹紪绋嬩腑锛屾妸鏂囦欢鐪嬩綔涓绯诲垪镄勫瓧鑺傛祦銆
C璇瑷浣跨敤鏂囦欢娴佺殑姒傚康𨱒ュ勭悊鏂囦欢銆傛枃浠舵祦鏄涓绉嶆娊璞$殑姒傚康锛岃〃绀轰粠鏂囦欢璇诲彇鏁版嵁鎴栧悜鏂囦欢鍐椤叆鏁版嵁镄勮繃绋嬨傞氲繃浣跨敤鏂囦欢娴侊纴C璇瑷绋嫔簭鍙浠ユ柟渚垮湴璇诲彇鍜屽啓鍏ユ枃浠躲
C璇瑷涓鎻愪緵stdio.h澶存枃浠朵腑镄勫嚱鏁帮纴鐢ㄤ簬鍒涘缓銆佹墦寮銆佽诲啓鍜屽叧闂鏂囦欢銆傝繖浜涘嚱鏁板彲浠ヤ笌鏂囦欢娴佷竴璧蜂娇鐢锛屼互镓ц屽悇绉嶆枃浠舵搷浣溿备緥濡傦纴fopen锛堬级鍑芥暟鐢ㄤ簬镓揿紑涓涓鏂囦欢骞惰繑锲炰竴涓鏂囦欢娴侊纴fread锛堬级鍑芥暟鐢ㄤ簬浠庢枃浠朵腑璇诲彇鏁版嵁锛宖write锛堬级鍑芥暟鐢ㄤ簬钖戞枃浠朵腑鍐椤叆鏁版嵁锛宖close锛堬级鍑芥暟鐢ㄤ簬鍏抽棴鏂囦欢娴併
锲犳わ纴鍦–璇瑷涓锛屾枃浠惰鐪嬩綔鏄涓绯诲垪瀛楄妭镄勬祦锛岄氲繃浣跨敤鏂囦欢娴佸拰鐩稿叧鍑芥暟锛屽彲浠ユ柟渚垮湴璇诲彇鍜屽啓鍏ユ枃浠躲
❹ c++涓濡备綍浣跨敤鏂囦欢娴
C++鏂囦欢娴
鍦–++涓锛屾湁涓涓狲tream杩欎釜绫伙纴镓链夌殑I/O閮戒互杩欎釜钬沧祦钬濈被涓哄熀纭镄勶纴鍖呮嫭鎴戜滑瑕佽よ瘑镄勬枃浠禝/O锛宻tream杩欎釜绫绘湁涓や釜閲嶈佺殑杩愮畻绗︼细
1銆佹彃鍏ュ櫒(<<)
钖戞祦杈揿嚭鏁版嵁銆傛瘆濡傝寸郴缁熸湁涓涓榛樿ょ殑镙囧嗳杈揿嚭娴(cout)锛屼竴鑸𨱍呭喌涓嫔氨鏄鎸囩殑鏄剧ず鍣锛屾墍浠ワ纴cout<<"Write
Stdout"<<'n';灏辫〃绀烘妸瀛楃︿覆"Write
Stdout"鍜屾崲琛屽瓧绗('n')杈揿嚭鍒版爣鍑呜緭鍑烘祦銆
2銆佹瀽鍙栧櫒(>>)
浠庢祦涓杈揿叆鏁版嵁銆傛瘆濡傝寸郴缁熸湁涓涓榛樿ょ殑镙囧嗳杈揿叆娴(cin)锛屼竴鑸𨱍呭喌涓嫔氨鏄鎸囩殑阌鐩桡纴镓浠ワ纴cin>>x;灏辫〃绀轰粠镙囧嗳杈揿叆娴佷腑璇诲彇涓涓鎸囧畾绫诲瀷(鍗冲彉閲弜镄勭被鍨)镄勬暟鎹銆
鍦–++涓锛屽规枃浠剁殑镎崭綔鏄阃氲繃stream镄勫瓙绫簖stream(file
stream)𨱒ュ疄鐜扮殑锛屾墍浠ワ纴瑕佺敤杩欑嶆柟寮忔搷浣沧枃浠讹纴灏卞繀椤诲姞鍏ュご鏂囦欢fstream.h銆备笅闱㈠氨鎶婃ょ被镄勬枃浠舵搷浣滆繃绋嬩竴涓阆撴潵銆
涓銆佹墦寮鏂囦欢
鍦╢stream绫讳腑锛屾湁涓涓鎴愬憳鍑芥暟open()锛屽氨鏄鐢ㄦ潵镓揿紑鏂囦欢镄勶纴鍏跺师鍨嬫槸锛
void
open(const
char*
filename,int
mode,int
access);
鍙傛暟锛
filename锛
瑕佹墦寮镄勬枃浠跺悕
mode锛
瑕佹墦寮鏂囦欢镄勬柟寮
access锛
镓揿紑鏂囦欢镄勫睘镐
镓揿紑鏂囦欢镄勬柟寮忓湪绫籭os(鏄镓链夋祦寮廔/O绫荤殑锘虹被)涓瀹氢箟锛屽父鐢ㄧ殑鍊煎备笅锛
ios::app锛
浠ヨ拷锷犵殑鏂瑰纺镓揿紑鏂囦欢
ios::ate锛
鏂囦欢镓揿紑钖庡畾浣嶅埌鏂囦欢灏撅纴ios:app灏卞寘钖链夋ゅ睘镐
ios::binary锛
浠ヤ簩杩涘埗鏂瑰纺镓揿紑鏂囦欢锛岀己鐪佺殑鏂瑰纺鏄鏂囨湰鏂瑰纺銆备袱绉嶆柟寮忕殑鍖哄埆瑙佸墠鏂
ios::in锛
鏂囦欢浠ヨ緭鍏ユ柟寮忔墦寮
ios::out锛
鏂囦欢浠ヨ緭鍑烘柟寮忔墦寮
ios::nocreate锛
涓嶅缓绔嬫枃浠讹纴镓浠ユ枃浠朵笉瀛桦湪镞舵墦寮澶辫触
ios::noreplace锛氢笉瑕嗙洊鏂囦欢锛屾墍浠ユ墦寮鏂囦欢镞跺傛灉鏂囦欢瀛桦湪澶辫触
ios::trunc锛
濡傛灉鏂囦欢瀛桦湪锛屾妸鏂囦欢闀垮害璁句负0
鍙浠ョ敤钬沧垨钬濇妸浠ヤ笂灞炴ц繛鎺ヨ捣𨱒ワ纴濡俰os::out|ios::binary
镓揿紑鏂囦欢镄勫睘镐у彇鍊兼槸锛
0锛氭櫘阃氭枃浠讹纴镓揿紑璁块梾
1锛氩彧璇绘枃浠
2锛氶殣钖鏂囦欢
4锛氱郴缁熸枃浠
鍙浠ョ敤钬沧垨钬濇垨钥呪+钬濇妸浠ヤ笂灞炴ц繛鎺ヨ捣𨱒
锛屽3鎴1|2灏辨槸浠ュ彧璇诲拰闅愬惈灞炴ф墦寮鏂囦欢銆
渚嫔傦细浠ヤ簩杩涘埗杈揿叆鏂瑰纺镓揿紑鏂囦欢c:config.sys
fstream
file1;
file1.open("c:config.sys",ios::binary|ios::in,0);
濡傛灉open鍑芥暟鍙链夋枃浠跺悕涓涓鍙傛暟锛屽垯鏄浠ヨ/鍐欐櫘阃氭枃浠舵墦寮锛屽嵆锛
file1.open("c:config.sys");<=>file1.open("c:config.sys",ios::in|ios::out,0);
鍙﹀栵纴fstream杩樻湁鍜宱pen()涓镙风殑鏋勯犲嚱鏁帮纴瀵逛簬涓娄緥锛屽湪瀹氢箟镄勬椂鍊椤氨鍙浠ユ墦寮鏂囦欢浜嗭细
fstream
file1("c:config.sys");
鐗瑰埆鎻愬嚭镄勬槸锛宖stream链変袱涓瀛愮被锛歩fstream(input
file
stream)鍜宱fstream(outpu
file
stream)锛宨fstream榛樿や互杈揿叆鏂瑰纺镓揿紑鏂囦欢锛岃宱fstream榛樿や互杈揿嚭鏂瑰纺镓揿紑鏂囦欢銆
ifstream
file2("c:pdos.def");//浠ヨ緭鍏ユ柟寮忔墦寮鏂囦欢
ofstream
file3("c:x.123");//浠ヨ緭鍑烘柟寮忔墦寮鏂囦欢
镓浠ワ纴鍦ㄥ疄闄呭簲鐢ㄤ腑锛屾牴鎹闇瑕佺殑涓嶅悓锛岄夋嫨涓嶅悓镄勭被𨱒ュ畾涔夛细濡傛灉𨱍充互杈揿叆鏂瑰纺镓揿紑锛屽氨鐢╥fstream𨱒ュ畾涔夛绂濡傛灉𨱍充互杈揿嚭鏂瑰纺镓揿紑锛屽氨鐢╫fstream𨱒ュ畾涔夛绂濡傛灉𨱍充互杈揿叆/杈揿嚭鏂瑰纺𨱒ユ墦寮锛屽氨鐢╢stream𨱒ュ畾涔夈
浜屻佸叧闂鏂囦欢
镓揿紑镄勬枃浠朵娇鐢ㄥ畬鎴愬悗涓瀹氲佸叧闂锛宖stream鎻愪緵浜嗘垚锻桦嚱鏁瘫lose()𨱒ュ畬鎴愭ゆ搷浣滐纴濡傦细file1.close();灏辨妸file1鐩歌繛镄勬枃浠跺叧闂銆
涓夈佽诲啓鏂囦欢
璇诲啓鏂囦欢鍒嗕负鏂囨湰鏂囦欢鍜屼簩杩涘埗鏂囦欢镄勮诲彇锛屽逛簬鏂囨湰鏂囦欢镄勮诲彇姣旇缉绠鍗曪纴鐢ㄦ彃鍏ュ櫒鍜屾瀽鍙栧櫒灏卞彲浠ヤ简锛涜屽逛簬浜岃繘鍒剁殑璇诲彇灏辫佸嶆潅浜涳纴涓嬭佸氨璇︾粏镄勪粙缁嶈繖涓ょ嶆柟寮
1銆佹枃链鏂囦欢镄勮诲啓
鏂囨湰鏂囦欢镄勮诲啓寰堢亩鍗曪细鐢ㄦ彃鍏ュ櫒(<<)钖戞枃浠惰緭鍑猴绂鐢ㄦ瀽鍙栧櫒(>>)浠庢枃浠惰緭鍏ャ傚亣璁緁ile1鏄浠ヨ緭鍏ユ柟寮忔墦寮锛宖ile2浠ヨ緭鍑烘墦寮銆傜ず渚嫔备笅锛
file2<<"I
Love
You";//钖戞枃浠跺啓鍏ュ瓧绗︿覆"I
Love
You"
int
i;
file1>>i;//浠庢枃浠惰緭鍏ヤ竴涓鏁存暟鍊笺
杩欑嶆柟寮忚缮链変竴绉岖亩鍗旷殑镙煎纺鍖栬兘锷涳纴姣斿傚彲浠ユ寚瀹氲緭鍑轰负16杩涘埗绛夌瓑锛屽叿浣撶殑镙煎纺链変互涓嬩竴浜
镎岖旱绗
锷熻兘
杈揿叆/杈揿嚭
dec
镙煎纺鍖栦负鍗佽繘鍒舵暟鍊兼暟鎹
杈揿叆鍜岃緭鍑
endl
杈揿嚭涓涓鎹㈣岀﹀苟鍒锋柊姝ゆ祦
杈揿嚭
ends
杈揿嚭涓涓绌哄瓧绗
杈揿嚭
hex
镙煎纺鍖栦负鍗佸叚杩涘埗鏁板兼暟鎹
杈揿叆鍜岃緭鍑
oct
镙煎纺鍖栦负鍏杩涘埗鏁板兼暟鎹
杈揿叆鍜岃緭鍑
setpxecision(int
p)
璁剧疆娴镣规暟镄勭簿搴︿綅鏁
杈揿嚭
姣斿傝佹妸123褰扑綔鍗佸叚杩涘埗杈揿嚭锛歠ile1<<hex<<123;瑕佹妸3.1415926浠5浣岖簿搴﹁緭鍑猴细file1<<setpxecision(5)<<3.1415926銆
2銆佷簩杩涘埗鏂囦欢镄勮诲啓
鈶爌ut()
put()鍑芥暟钖戞祦鍐椤叆涓涓瀛楃︼纴鍏跺师鍨嬫槸ofstream
&put(char
ch)锛屼娇鐢ㄤ篃姣旇缉绠鍗曪纴濡俧ile1.put('c');灏辨槸钖戞祦鍐欎竴涓瀛楃'c'銆
鈶get()
get()鍑芥暟姣旇缉𨱔垫椿锛屾湁3绉嶅父鐢ㄧ殑閲嶈浇褰㈠纺锛
涓绉嶅氨鏄鍜宲ut()瀵瑰簲镄勫舰寮忥细ifstream
&get(char
&ch);锷熻兘鏄浠庢祦涓璇诲彇涓涓瀛楃︼纴缁撴灉淇濆瓨鍦ㄥ紩鐢╟h涓锛屽傛灉鍒版枃浠跺熬锛岃繑锲炵┖瀛楃︺傚俧ile2.get(x);琛ㄧず浠庢枃浠朵腑璇诲彇涓涓瀛楃︼纴骞舵妸璇诲彇镄勫瓧绗︿缭瀛桦湪x涓銆
鍙︿竴绉嶉吨杞藉舰寮忕殑铡熷瀷鏄锛
int
get();杩欑嶅舰寮忔槸浠庢祦涓杩斿洖涓涓瀛楃︼纴濡傛灉鍒拌揪鏂囦欢灏撅纴杩斿洖EOF锛屽倄=file2.get();鍜屼笂渚嫔姛鑳芥槸涓镙风殑銆
杩樻湁涓绉嶅舰寮忕殑铡熷瀷鏄锛歩fstream
&get(char
*buf,int
num,char
delim='n')锛涜繖绉嶅舰寮忔妸瀛楃﹁诲叆鐢
buf
鎸囧悜镄勬暟缁勶纴鐩村埌璇诲叆浜
num
涓瀛楃︽垨阆囧埌浜嗙敱
delim
鎸囧畾镄勫瓧绗︼纴濡傛灉娌′娇鐢
delim
杩欎釜鍙傛暟锛屽皢浣跨敤缂虹渷鍊兼崲琛岀'n'銆备緥濡傦细
file2.get(str1,127,'A');//浠庢枃浠朵腑璇诲彇瀛楃﹀埌瀛楃︿覆str1锛屽綋阆囧埌瀛楃'A'鎴栬诲彇浜127涓瀛楃︽椂缁堟銆
鈶㈣诲啓鏁版嵁鍧
瑕佽诲啓浜岃繘鍒舵暟鎹鍧楋纴浣跨敤鎴愬憳鍑芥暟read()鍜寃rite()鎴愬憳鍑芥暟锛屽畠浠铡熷瀷濡备笅锛
read(unsigned
char
*buf,int
num);
write(const
unsigned
char
*buf,int
num);
read()浠庢枃浠朵腑璇诲彇
num
涓瀛楃﹀埌
buf
鎸囧悜镄勭紦瀛树腑锛屽傛灉鍦ㄨ缮链璇诲叆
num
涓瀛楃︽椂灏卞埌浜嗘枃浠跺熬锛屽彲浠ョ敤鎴愬憳鍑芥暟
int
gcount();𨱒ュ彇寰楀疄闄呰诲彇镄勫瓧绗︽暟锛涜
write()
浠巄uf
鎸囧悜镄勭紦瀛桦啓
num
涓瀛楃﹀埌鏂囦欢涓锛屽煎缑娉ㄦ剰镄勬槸缂揿瓨镄勭被鍨嬫槸
unsigned
char
*锛屾湁镞跺彲鑳介渶瑕佺被鍨嬭浆鎹銆
渚嬶细
unsigned
char
str1[]="I
Love
You";
int
n[5];
ifstream
in("xxx.xxx");
ofstream
out("yyy.yyy");
out.write(str1,strlen(str1));//鎶婂瓧绗︿覆str1鍏ㄩ儴鍐椤埌yyy.yyy涓
in.read((unsigned
char*)n,sizeof(n));//浠巟xx.xxx涓璇诲彇鎸囧畾涓鏁存暟锛屾敞镒忕被鍨嬭浆鎹
in.close();out.close();
锲涖佹娴婨OF
鎴愬憳鍑芥暟eof()鐢ㄦ潵妫娴嬫槸钖﹀埌杈炬枃浠跺熬锛屽傛灉鍒拌揪鏂囦欢灏捐繑锲为潪0鍊硷纴钖﹀垯杩斿洖0銆傚师鍨嬫槸int
eof();
渚嬶细
if(in.eof())ShowMessage("宸茬粡鍒拌揪鏂囦欢灏撅紒");
浜斻佹枃浠跺畾浣
鍜孋镄勬枃浠舵搷浣沧柟寮忎笉钖岀殑鏄锛孋++
I/O绯荤粺绠$悊涓や釜涓庝竴涓鏂囦欢鐩歌仈绯荤殑鎸囬拡銆备竴涓鏄璇绘寚阍堬纴瀹冭存槑杈揿叆镎崭綔鍦ㄦ枃浠朵腑镄勪綅缃锛涘彟涓涓鏄鍐欐寚阍堬纴瀹冧笅娆″啓镎崭綔镄勪綅缃銆傛疮娆℃墽琛岃緭鍏ユ垨杈揿嚭镞讹纴鐩稿簲镄勬寚阍堣嚜锷ㄥ彉鍖栥傛墍浠ワ纴C++镄勬枃浠跺畾浣嶅垎涓鸿讳綅缃鍜屽啓浣岖疆镄勫畾浣嶏纴瀵瑰簲镄勬垚锻桦嚱鏁版槸
seekg()鍜
seekp()锛宻eekg()鏄璁剧疆璇讳綅缃锛宻eekp鏄璁剧疆鍐欎綅缃銆傚畠浠链阃氱敤镄勫舰寮忓备笅锛
istream
&seekg(streamoff
offset,seek_dir
origin);
ostream
&seekp(streamoff
offset,seek_dir
origin);
streamoff瀹氢箟浜
iostream.h
涓锛屽畾涔夋湁锅忕Щ閲
offset
镓鑳藉彇寰楃殑链澶у硷纴seek_dir
琛ㄧず绉诲姩镄勫熀鍑嗕綅缃锛屾槸涓涓链変互涓嫔肩殑鏋氢妇锛
ios::beg锛
鏂囦欢寮澶
ios::cur锛
鏂囦欢褰揿墠浣岖疆
ios::end锛
鏂囦欢缁揿熬
杩欎袱涓鍑芥暟涓鑸鐢ㄤ簬浜岃繘鍒舵枃浠讹纴锲犱负鏂囨湰鏂囦欢浼氩洜涓虹郴缁熷瑰瓧绗︾殑瑙i喷钥屽彲鑳戒笌棰勬兂镄勫间笉钖屻
渚嬶细
file1.seekg(1234,ios::cur);//鎶婃枃浠剁殑璇绘寚阍堜粠褰揿墠浣岖疆钖戝悗绉1234涓瀛楄妭
file2.seekp(1234,ios::beg);//鎶婃枃浠剁殑鍐欐寚阍堜粠鏂囦欢寮澶村悜钖庣Щ1234涓瀛楄妭
濡傛灉vc缂栫▼镄勮瘽链濂戒娇鐢–File绫荤瓑镟村姞鏂逛究浜庢枃浠舵搷浣</CA>