当前位置:首页 » 编程语言 » c语言修改文件内容

c语言修改文件内容

发布时间: 2024-07-19 22:06:56

㈠ 镐庝箞瀵笴璇瑷涓镄勬枃浠惰繘琛屽唴瀹规坊锷狅纴鍒犻櫎锛屾煡镓剧殑镎崭綔锛

c璇瑷鏂囦欢镎崭綔

13.3 鏂囦欢镄勬墦寮涓庡叧闂
鏂囦欢鍦ㄨ繘琛岃诲啓镎崭綔涔嫔墠瑕佸厛镓揿紑锛屼娇鐢ㄥ畬姣曡佸叧闂銆傛墍璋撴墦寮鏂囦欢锛屽疄闄呬笂鏄寤虹珛鏂囦欢镄勫悇绉嶆湁鍏充俊鎭锛屽苟浣挎枃浠舵寚阍堟寚钖戣ユ枃浠讹纴浠ヤ究杩涜屽叾瀹冩搷浣溿傚叧闂鏂囦欢鍒欐柇寮鎸囬拡涓庢枃浠朵箣闂寸殑镵旂郴锛屼篃灏辩佹㈠啀瀵硅ユ枃浠惰繘琛屾搷浣溿

鍦–璇瑷涓锛屾枃浠舵搷浣滈兘鏄鐢卞簱鍑芥暟𨱒ュ畬鎴愮殑銆傚湪链绔犲唴灏嗕粙缁崭富瑕佺殑鏂囦欢镎崭綔鍑芥暟銆

13.3.1 鏂囦欢镄勬墦寮(fopen鍑芥暟)
fopen鍑芥暟鐢ㄦ潵镓揿紑涓涓鏂囦欢锛屽叾璋幂敤镄勪竴鑸褰㈠纺涓猴细

鏂囦欢鎸囬拡钖=fopen(鏂囦欢钖,浣跨敤鏂囦欢鏂瑰纺);

鍏朵腑锛

钬沧枃浠舵寚阍埚悕钬濆繀椤绘槸琚璇存槑涓篎ILE 绫诲瀷镄勬寚阍埚彉閲忥绂

钬沧枃浠跺悕钬濇槸琚镓揿紑鏂囦欢镄勬枃浠跺悕锛

钬滀娇鐢ㄦ枃浠舵柟寮忊濇槸鎸囨枃浠剁殑绫诲瀷鍜屾搷浣滆佹眰銆

钬沧枃浠跺悕钬濇槸瀛楃︿覆甯搁噺鎴栧瓧绗︿覆鏁扮粍銆

渚嫔傦细

FILE *fp锛

fp=("file a","r");

鍏舵剰涔夋槸鍦ㄥ綋鍓岖洰褰曚笅镓揿紑鏂囦欢file a锛屽彧鍏佽歌繘琛屸滆烩濇搷浣滐纴骞朵娇fp鎸囧悜璇ユ枃浠躲

鍙埚傦细

FILE *fphzk

fphzk=("c:\\hzk16","rb")

鍏舵剰涔夋槸镓揿紑C椹卞姩鍣ㄧ佺洏镄勬牴鐩褰曚笅镄勬枃浠秇zk16锛岃繖鏄涓涓浜岃繘鍒舵枃浠讹纴鍙鍏佽告寜浜岃繘鍒舵柟寮忚繘琛岃绘搷浣溿备袱涓鍙嶆枩绾库\\ 钬濅腑镄勭涓涓琛ㄧず杞涔夊瓧绗︼纴绗浜屼釜琛ㄧず镙圭洰褰曘

浣跨敤鏂囦欢镄勬柟寮忓叡链12绉嶏纴涓嬮溃缁椤嚭浜嗗畠浠镄勭﹀彿鍜屾剰涔夈

鏂囦欢浣跨敤鏂瑰纺
镒忎箟

钬渞t钬
鍙璇绘墦寮涓涓鏂囨湰鏂囦欢锛屽彧鍏佽歌绘暟鎹

钬渨t钬
鍙鍐欐墦寮鎴栧缓绔嬩竴涓鏂囨湰鏂囦欢锛屽彧鍏佽稿啓鏁版嵁

钬渁t钬
杩藉姞镓揿紑涓涓鏂囨湰鏂囦欢锛屽苟鍦ㄦ枃浠舵汤灏惧啓鏁版嵁

钬渞b钬
鍙璇绘墦寮涓涓浜岃繘鍒舵枃浠讹纴鍙鍏佽歌绘暟鎹

钬渨b钬
鍙鍐欐墦寮鎴栧缓绔嬩竴涓浜岃繘鍒舵枃浠讹纴鍙鍏佽稿啓鏁版嵁

钬渁b钬
杩藉姞镓揿紑涓涓浜岃繘鍒舵枃浠讹纴骞跺湪鏂囦欢链灏惧啓鏁版嵁

钬渞t+钬
璇诲啓镓揿紑涓涓鏂囨湰鏂囦欢锛屽厑璁歌诲拰鍐

钬渨t+钬
璇诲啓镓揿紑鎴栧缓绔嬩竴涓鏂囨湰鏂囦欢锛屽厑璁歌诲啓

钬渁t+钬
璇诲啓镓揿紑涓涓鏂囨湰鏂囦欢锛屽厑璁歌伙纴鎴栧湪鏂囦欢链杩藉姞鏁版嵁

钬渞b+钬
璇诲啓镓揿紑涓涓浜岃繘鍒舵枃浠讹纴鍏佽歌诲拰鍐

钬渨b+钬
璇诲啓镓揿紑鎴栧缓绔嬩竴涓浜岃繘鍒舵枃浠讹纴鍏佽歌诲拰鍐

钬渁b+钬
璇诲啓镓揿紑涓涓浜岃繘鍒舵枃浠讹纴鍏佽歌伙纴鎴栧湪鏂囦欢链杩藉姞鏁版嵁

瀵逛簬鏂囦欢浣跨敤鏂瑰纺链変互涓嫔嚑镣硅存槑锛

1) 鏂囦欢浣跨敤鏂瑰纺鐢眗,w,a,t,b锛+鍏涓瀛楃︽嫾鎴愶纴钖勫瓧绗︾殑钖涔夋槸锛

r(read): 璇

w(write): 鍐

a(append): 杩藉姞

t(text): 鏂囨湰鏂囦欢锛屽彲鐪佺暐涓嶅啓

b(banary): 浜岃繘鍒舵枃浠

+: 璇诲拰鍐

2) 鍑$敤钬渞钬濇墦寮涓涓鏂囦欢镞讹纴璇ユ枃浠跺繀椤诲凡缁忓瓨鍦锛屼笖鍙鑳戒粠璇ユ枃浠惰诲嚭銆

3) 鐢ㄢ渨钬濇墦寮镄勬枃浠跺彧鑳藉悜璇ユ枃浠跺啓鍏ャ傝嫢镓揿紑镄勬枃浠朵笉瀛桦湪锛屽垯浠ユ寚瀹氱殑鏂囦欢钖嶅缓绔嬭ユ枃浠讹纴鑻ユ墦寮镄勬枃浠跺凡缁忓瓨鍦锛屽垯灏呜ユ枃浠跺垹铡伙纴閲嶅缓涓涓鏂版枃浠躲

4) 鑻ヨ佸悜涓涓宸插瓨鍦ㄧ殑鏂囦欢杩藉姞鏂扮殑淇℃伅锛屽彧鑳界敤钬渁钬濇柟寮忔墦寮鏂囦欢銆备絾姝ゆ椂璇ユ枃浠跺繀椤绘槸瀛桦湪镄勶纴钖﹀垯灏嗕细鍑洪敊銆

5) 鍦ㄦ墦寮涓涓鏂囦欢镞讹纴濡傛灉鍑洪敊锛宖open灏呜繑锲炰竴涓绌烘寚阍埚糔ULL銆傚湪绋嫔簭涓鍙浠ョ敤杩欎竴淇℃伅𨱒ュ垽鍒鏄钖﹀畬鎴愭墦寮鏂囦欢镄勫伐浣滐纴骞朵綔鐩稿簲镄勫勭悊銆傚洜姝ゅ父鐢ㄤ互涓嬬▼搴忔垫墦寮鏂囦欢锛

6) if((fp=fopen("c:\\hzk16","rb")==NULL)

{

printf("\nerror on open c:\\hzk16 file!");

getch();

exit(1);

}

杩欐电▼搴忕殑镒忎箟鏄锛屽傛灉杩斿洖镄勬寚阍堜负绌猴纴琛ㄧず涓嶈兘镓揿紑C鐩樻牴鐩褰曚笅镄删zk16鏂囦欢锛屽垯缁椤嚭鎻愮ず淇℃伅钬渆rror on open c:\ hzk16 file!钬濓纴涓嬩竴琛実etch()镄勫姛鑳芥槸浠庨敭鐩樿緭鍏ヤ竴涓瀛楃︼纴浣嗕笉鍦ㄥ睆骞曚笂鏄剧ず銆傚湪杩欓噷锛岃ヨ岀殑浣灭敤鏄绛夊緟锛屽彧链夊綋鐢ㄦ埛浠庨敭鐩樻暡浠讳竴阌镞讹纴绋嫔簭镓岖户缁镓ц岋纴锲犳ょ敤鎴峰彲鍒╃敤杩欎釜绛夊緟镞堕棿阒呰诲嚭阌欐彁绀恒傛暡阌钖庢墽琛宔xit(1)阃鍑虹▼搴忋

7) 鎶娄竴涓鏂囨湰鏂囦欢璇诲叆鍐呭瓨镞讹纴瑕佸皢ASCII镰佽浆鎹㈡垚浜岃繘鍒剁爜锛岃屾妸鏂囦欢浠ユ枃链鏂瑰纺鍐椤叆纾佺洏镞讹纴涔熻佹妸浜岃繘鍒剁爜杞鎹㈡垚ASCII镰侊纴锲犳ゆ枃链鏂囦欢镄勮诲啓瑕佽姳璐硅缉澶氱殑杞鎹㈡椂闂淬傚逛簩杩涘埗鏂囦欢镄勮诲啓涓嶅瓨鍦ㄨ繖绉嶈浆鎹銆

8) 镙囧嗳杈揿叆鏂囦欢(阌鐩)锛屾爣鍑呜緭鍑烘枃浠(鏄剧ず鍣)锛屾爣鍑嗗嚭阌栾緭鍑(鍑洪敊淇℃伅)鏄鐢辩郴缁熸墦寮镄勶纴鍙鐩存帴浣跨敤銆

13.3.2 鏂囦欢鍏抽棴鍑芥暟锛坒close鍑芥暟锛
鏂囦欢涓镞︿娇鐢ㄥ畬姣曪纴搴旂敤鍏抽棴鏂囦欢鍑芥暟鎶婃枃浠跺叧闂锛屼互阆垮厤鏂囦欢镄勬暟鎹涓㈠け绛夐敊璇銆

fclose鍑芥暟璋幂敤镄勪竴鑸褰㈠纺鏄锛

fclose(鏂囦欢鎸囬拡)锛

渚嫔傦细

fclose(fp);

姝e父瀹屾垚鍏抽棴鏂囦欢镎崭綔镞讹纴fclose鍑芥暟杩斿洖鍊间负0銆傚傝繑锲为潪闆跺煎垯琛ㄧず链夐敊璇鍙戠敓銆

13.4 鏂囦欢镄勮诲啓
瀵规枃浠剁殑璇诲拰鍐欐槸链甯哥敤镄勬枃浠舵搷浣溿傚湪C璇瑷涓鎻愪緵浜嗗氱嶆枃浠惰诲啓镄勫嚱鏁帮细

路瀛楃﹁诲啓鍑芥暟 锛歠getc鍜宖putc

路瀛楃︿覆璇诲啓鍑芥暟锛歠gets鍜宖puts

路鏁版嵁鍧楄诲啓鍑芥暟锛歠reed鍜宖write

路镙煎纺鍖栬诲啓鍑芥暟锛歠scanf鍜宖prinf

涓嬮溃鍒嗗埆浜堜互浠嬬粛銆备娇鐢ㄤ互涓婂嚱鏁伴兘瑕佹眰鍖呭惈澶存枃浠秙tdio.h銆

13.4.1 瀛楃﹁诲啓鍑芥暟fgetc鍜宖putc
瀛楃﹁诲啓鍑芥暟鏄浠ュ瓧绗(瀛楄妭)涓哄崟浣岖殑璇诲啓鍑芥暟銆傛疮娆″彲浠庢枃浠惰诲嚭鎴栧悜鏂囦欢鍐椤叆涓涓瀛楃︺

1. 璇诲瓧绗﹀嚱鏁癴getc

fgetc鍑芥暟镄勫姛鑳芥槸浠庢寚瀹氱殑鏂囦欢涓璇讳竴涓瀛楃︼纴鍑芥暟璋幂敤镄勫舰寮忎负锛

瀛楃﹀彉閲=fgetc(鏂囦欢鎸囬拡)锛

渚嫔傦细

ch=fgetc(fp);

鍏舵剰涔夋槸浠庢墦寮镄勬枃浠秄p涓璇诲彇涓涓瀛楃﹀苟阃佸叆ch涓銆

瀵逛簬fgetc鍑芥暟镄勪娇鐢ㄦ湁浠ヤ笅鍑犵偣璇存槑锛

1) 鍦╢getc鍑芥暟璋幂敤涓锛岃诲彇镄勬枃浠跺繀椤绘槸浠ヨ绘垨璇诲啓鏂瑰纺镓揿紑镄勚

2) 璇诲彇瀛楃︾殑缁撴灉涔熷彲浠ヤ笉钖戝瓧绗﹀彉閲忚祴鍊硷纴

渚嫔傦细

fgetc(fp);

浣嗘槸璇诲嚭镄勫瓧绗︿笉鑳戒缭瀛樸

3) 鍦ㄦ枃浠跺唴閮ㄦ湁涓涓浣岖疆鎸囬拡銆傜敤𨱒ユ寚钖戞枃浠剁殑褰揿墠璇诲啓瀛楄妭銆傚湪鏂囦欢镓揿紑镞讹纴璇ユ寚阍堟绘槸鎸囧悜鏂囦欢镄勭涓涓瀛楄妭銆备娇鐢╢getc 鍑芥暟钖庯纴璇ヤ綅缃鎸囬拡灏嗗悜钖庣Щ锷ㄤ竴涓瀛楄妭銆 锲犳ゅ彲杩炵画澶氭′娇鐢╢getc鍑芥暟锛岃诲彇澶氢釜瀛楃︺傚簲娉ㄦ剰鏂囦欢鎸囬拡鍜屾枃浠跺唴閮ㄧ殑浣岖疆鎸囬拡涓嶆槸涓锲炰簨銆傛枃浠舵寚阍堟槸鎸囧悜鏁翠釜鏂囦欢镄勶纴椤诲湪绋嫔簭涓瀹氢箟璇存槑锛屽彧瑕佷笉閲嶆柊璧嫔硷纴鏂囦欢鎸囬拡镄勫兼槸涓嶅彉镄勚傛枃浠跺唴閮ㄧ殑浣岖疆鎸囬拡鐢ㄤ互鎸囩ず鏂囦欢鍐呴儴镄勫綋鍓嶈诲啓浣岖疆锛屾疮璇诲啓涓娆★纴璇ユ寚阍埚潎钖戝悗绉诲姩锛屽畠涓嶉渶鍦ㄧ▼搴忎腑瀹氢箟璇存槑锛岃屾槸鐢辩郴缁熻嚜锷ㄨ剧疆镄勚

銆愪緥13.1銆戣诲叆鏂囦欢c1.doc锛屽湪灞忓箷涓婅緭鍑恒

#include<stdio.h>

main()

{

FILE *fp;

char ch;

if((fp=fopen("d:\\jrzh\\example\\c1.txt","rt"))==NULL)

{

printf("\nCannot open file strike any key exit!");

getch();

exit(1);

}

ch=fgetc(fp);

while(ch!=EOF)

{

putchar(ch);

ch=fgetc(fp);

}

fclose(fp);

}
链渚嬬▼搴忕殑锷熻兘鏄浠庢枃浠朵腑阃愪釜璇诲彇瀛楃︼纴鍦ㄥ睆骞曚笂鏄剧ず銆傜▼搴忓畾涔変简鏂囦欢鎸囬拡fp,浠ヨ绘枃链鏂囦欢鏂瑰纺镓揿紑鏂囦欢钬渄:\\jrzh\\example\\ex1_1.c钬濓纴骞朵娇fp鎸囧悜璇ユ枃浠躲傚傛墦寮鏂囦欢鍑洪敊锛岀粰鍑烘彁绀哄苟阃鍑虹▼搴忋傜▼搴忕12琛屽厛璇诲嚭涓涓瀛楃︼纴铹跺悗杩涘叆寰鐜锛屽彧瑕佽诲嚭镄勫瓧绗︿笉鏄鏂囦欢缁撴潫镙囧织(姣忎釜鏂囦欢链链変竴缁撴潫镙囧织EOF)灏辨妸璇ュ瓧绗︽樉绀哄湪灞忓箷涓婏纴鍐嶈诲叆涓嬩竴瀛楃︺傛疮璇讳竴娆★纴鏂囦欢鍐呴儴镄勪綅缃鎸囬拡钖戝悗绉诲姩涓涓瀛楃︼纴鏂囦欢缁撴潫镞讹纴璇ユ寚阍堟寚钖慐OF銆傛墽琛屾湰绋嫔簭灏嗘樉绀烘暣涓鏂囦欢銆

2. 鍐椤瓧绗﹀嚱鏁癴putc

fputc鍑芥暟镄勫姛鑳芥槸鎶娄竴涓瀛楃﹀啓鍏ユ寚瀹氱殑鏂囦欢涓锛屽嚱鏁拌皟鐢ㄧ殑褰㈠纺涓猴细

fputc(瀛楃﹂噺锛屾枃浠舵寚阍)锛

鍏朵腑锛屽緟鍐椤叆镄勫瓧绗﹂噺鍙浠ユ槸瀛楃﹀父閲忔垨鍙橀噺锛屼緥濡傦细

fputc('a',fp);

鍏舵剰涔夋槸鎶婂瓧绗a鍐椤叆fp镓鎸囧悜镄勬枃浠朵腑銆

瀵逛簬fputc鍑芥暟镄勪娇鐢ㄤ篃瑕佽存槑鍑犵偣锛

1) 琚鍐椤叆镄勬枃浠跺彲浠ョ敤鍐欍佽诲啓銆佽拷锷犳柟寮忔墦寮锛岀敤鍐欐垨璇诲啓鏂瑰纺镓揿紑涓涓宸插瓨鍦ㄧ殑鏂囦欢镞跺皢娓呴櫎铡熸湁镄勬枃浠跺唴瀹癸纴鍐椤叆瀛楃︿粠鏂囦欢棣栧紑濮嬨傚傞渶淇濈暀铡熸湁鏂囦欢鍐呭癸纴甯屾湜鍐椤叆镄勫瓧绗︿互鏂囦欢链寮濮嫔瓨鏀撅纴蹇呴’浠ヨ拷锷犳柟寮忔墦寮鏂囦欢銆傝鍐椤叆镄勬枃浠惰嫢涓嶅瓨鍦锛屽垯鍒涘缓璇ユ枃浠躲

2) 姣忓啓鍏ヤ竴涓瀛楃︼纴鏂囦欢鍐呴儴浣岖疆鎸囬拡钖戝悗绉诲姩涓涓瀛楄妭銆

3) fputc鍑芥暟链変竴涓杩斿洖鍊硷纴濡傚啓鍏ユ垚锷熷垯杩斿洖鍐椤叆镄勫瓧绗︼纴钖﹀垯杩斿洖涓涓狤OF銆傚彲鐢ㄦゆ潵鍒ゆ柇鍐椤叆鏄钖︽垚锷熴

銆愪緥13.2銆戜粠阌鐩樿緭鍏ヤ竴琛屽瓧绗︼纴鍐椤叆涓涓鏂囦欢锛屽啀鎶婅ユ枃浠跺唴瀹硅诲嚭鏄剧ず鍦ㄥ睆骞曚笂銆

#include<stdio.h>

main()

{

FILE *fp;

char ch;

if((fp=fopen("d:\\jrzh\\example\\string","wt+"))==NULL)

{

printf("Cannot open file strike any key exit!");

getch();

exit(1);

}

printf("input a string:\n");

ch=getchar();

while (ch!='\n')

{

fputc(ch,fp);

ch=getchar();

}

rewind(fp);

ch=fgetc(fp);

while(ch!=EOF)

{

putchar(ch);

ch=fgetc(fp);

}

printf("\n");

fclose(fp);

}

绋嫔簭涓绗6琛屼互璇诲啓鏂囨湰鏂囦欢鏂瑰纺镓揿紑鏂囦欢string銆傜▼搴忕13琛屼粠阌鐩樿诲叆涓涓瀛楃﹀悗杩涘叆寰鐜锛屽綋璇诲叆瀛楃︿笉涓哄洖杞︾︽椂锛屽垯鎶婅ュ瓧绗﹀啓鍏ユ枃浠朵箣涓锛岀劧钖庣户缁浠庨敭鐩樿诲叆涓嬩竴瀛楃︺傛疮杈揿叆涓涓瀛楃︼纴鏂囦欢鍐呴儴浣岖疆鎸囬拡钖戝悗绉诲姩涓涓瀛楄妭銆傚啓鍏ュ畬姣曪纴璇ユ寚阍埚凡鎸囧悜鏂囦欢链銆傚傝佹妸鏂囦欢浠庡ご璇诲嚭锛岄’鎶婃寚阍堢Щ钖戞枃浠跺ご锛岀▼搴忕19琛宺ewind鍑芥暟鐢ㄤ簬鎶奻p镓鎸囨枃浠剁殑鍐呴儴浣岖疆鎸囬拡绉诲埌鏂囦欢澶淬傜20镊25琛岀敤浜庤诲嚭鏂囦欢涓镄勪竴琛屽唴瀹广

銆愪緥13.3銆戞妸锻戒护琛屽弬鏁颁腑镄勫墠涓涓鏂囦欢钖嶆爣璇嗙殑鏂囦欢锛屽嶅埗鍒板悗涓涓鏂囦欢钖嶆爣璇嗙殑鏂囦欢涓锛屽傚懡浠よ屼腑鍙链変竴涓鏂囦欢钖嶅垯鎶婅ユ枃浠跺啓鍒版爣鍑呜緭鍑烘枃浠(鏄剧ず鍣)涓銆

#include<stdio.h>

main(int argc,char *argv[])

{

FILE *fp1,*fp2;

char ch;

if(argc==1)

{

printf("have not enter file name strike any key exit");

getch();

exit(0);

}

if((fp1=fopen(argv[1],"rt"))==NULL)

{

printf("Cannot open %s\n",argv[1]);

getch();

exit(1);

}

if(argc==2) fp2=stdout;

else if((fp2=fopen(argv[2],"wt+"))==NULL)

{

printf("Cannot open %s\n",argv[1]);

getch();

exit(1);

}

while((ch=fgetc(fp1))!=EOF)

fputc(ch,fp2);

fclose(fp1);

fclose(fp2);

}

链绋嫔簭涓哄甫鍙傜殑main鍑芥暟銆傜▼搴忎腑瀹氢箟浜嗕袱涓鏂囦欢鎸囬拡fp1鍜宖p2锛屽垎鍒鎸囧悜锻戒护琛屽弬鏁颁腑缁椤嚭镄勬枃浠躲傚傚懡浠よ屽弬鏁颁腑娌℃湁缁椤嚭鏂囦欢钖嶏纴鍒欑粰鍑烘彁绀轰俊鎭銆傜▼搴忕18琛岃〃绀哄傛灉鍙缁椤嚭涓涓鏂囦欢钖嶏纴鍒欎娇fp2鎸囧悜镙囧嗳杈揿嚭鏂囦欢(鍗虫樉绀哄櫒)銆傜▼搴忕25琛岃呖28琛岀敤寰鐜璇鍙ラ愪釜璇诲嚭鏂囦欢1涓镄勫瓧绗﹀啀阃佸埌鏂囦欢2涓銆傚啀娆¤繍琛屾椂锛岀粰鍑轰简涓涓鏂囦欢钖嶏纴鏁呰緭鍑虹粰镙囧嗳杈揿嚭鏂囦欢stdout锛屽嵆鍦ㄦ樉绀哄櫒涓婃樉绀烘枃浠跺唴瀹广傜涓夋¤繍琛岋纴缁椤嚭浜嗕簩涓鏂囦欢钖嶏纴锲犳ゆ妸string涓镄勫唴瀹硅诲嚭锛屽啓鍏ュ埌OK涔嬩腑銆傚彲鐢―OS锻戒护type鏄剧ずOK镄勫唴瀹广

13.4.2 瀛楃︿覆璇诲啓鍑芥暟fgets鍜宖puts
1. 璇诲瓧绗︿覆鍑芥暟fgets

鍑芥暟镄勫姛鑳芥槸浠庢寚瀹氱殑鏂囦欢涓璇讳竴涓瀛楃︿覆鍒板瓧绗︽暟缁勪腑锛屽嚱鏁拌皟鐢ㄧ殑褰㈠纺涓猴细

fgets(瀛楃︽暟缁勫悕,n,鏂囦欢鎸囬拡);

鍏朵腑镄刵鏄涓涓姝f暣鏁般傝〃绀轰粠鏂囦欢涓璇诲嚭镄勫瓧绗︿覆涓嶈秴杩 n-1涓瀛楃︺傚湪璇诲叆镄勬渶钖庝竴涓瀛楃﹀悗锷犱笂涓茬粨𨱒熸爣蹇'\0'銆

渚嫔傦细

fgets(str,n,fp);

镄勬剰涔夋槸浠巉p镓鎸囩殑鏂囦欢涓璇诲嚭n-1涓瀛楃﹂佸叆瀛楃︽暟缁剆tr涓銆

銆愪緥13.4銆戜粠string鏂囦欢涓璇诲叆涓涓钖10涓瀛楃︾殑瀛楃︿覆銆

#include<stdio.h>

main()

{

FILE *fp;

char str[11];

if((fp=fopen("d:\\jrzh\\example\\string","rt"))==NULL)

{

printf("\nCannot open file strike any key exit!");

getch();

exit(1);

}

fgets(str,11,fp);

printf("\n%s\n",str);

fclose(fp);

}

链渚嫔畾涔変简涓涓瀛楃︽暟缁剆tr鍏11涓瀛楄妭锛屽湪浠ヨ绘枃链鏂囦欢鏂瑰纺镓揿紑鏂囦欢string钖庯纴浠庝腑璇诲嚭10涓瀛楃﹂佸叆str鏁扮粍锛屽湪鏁扮粍链钖庝竴涓鍗曞厓鍐呭皢锷犱笂'\0'锛岀劧钖庡湪灞忓箷涓婃樉绀鸿緭鍑簊tr鏁扮粍銆傝緭鍑虹殑鍗佷釜瀛楃︽f槸渚13.1绋嫔簭镄勫墠鍗佷釜瀛楃︺

瀵筬gets鍑芥暟链変袱镣硅存槑锛

1) 鍦ㄨ诲嚭n-1涓瀛楃︿箣鍓嶏纴濡傞亣鍒颁简鎹㈣岀︽垨EOF锛屽垯璇诲嚭缁撴潫銆

2) fgets鍑芥暟涔熸湁杩斿洖鍊硷纴鍏惰繑锲炲兼槸瀛楃︽暟缁勭殑棣栧湴鍧銆

2. 鍐椤瓧绗︿覆鍑芥暟fputs

fputs鍑芥暟镄勫姛鑳芥槸钖戞寚瀹氱殑鏂囦欢鍐椤叆涓涓瀛楃︿覆锛屽叾璋幂敤褰㈠纺涓猴细

fputs(瀛楃︿覆,鏂囦欢鎸囬拡);

鍏朵腑瀛楃︿覆鍙浠ユ槸瀛楃︿覆甯搁噺锛屼篃鍙浠ユ槸瀛楃︽暟缁勫悕锛屾垨鎸囬拡鍙橀噺锛屼緥濡傦细

fputs(钬渁bcd钬,fp);

鍏舵剰涔夋槸鎶婂瓧绗︿覆钬渁bcd钬濆啓鍏fp镓鎸囩殑鏂囦欢涔嬩腑銆

銆愪緥13.5銆戝湪渚13.2涓寤虹珛镄勬枃浠秙tring涓杩藉姞涓涓瀛楃︿覆銆

#include<stdio.h>

main()

{

FILE *fp;

char ch,st[20];

if((fp=fopen("string","at+"))==NULL)

{

printf("Cannot open file strike any key exit!");

getch();

exit(1);

}

printf("input a string:\n");

scanf("%s",st);

fputs(st,fp);

rewind(fp);

ch=fgetc(fp);

while(ch!=EOF)

{

putchar(ch);

ch=fgetc(fp);

}

printf("\n");

fclose(fp);

}

链渚嬭佹眰鍦╯tring鏂囦欢链锷犲啓瀛楃︿覆锛屽洜姝わ纴鍦ㄧ▼搴忕6琛屼互杩藉姞璇诲啓鏂囨湰鏂囦欢镄勬柟寮忔墦寮鏂囦欢string銆傜劧钖庤緭鍏ュ瓧绗︿覆锛屽苟鐢╢puts鍑芥暟鎶婅ヤ覆鍐椤叆鏂囦欢string銆傚湪绋嫔簭15琛岀敤rewind鍑芥暟鎶婃枃浠跺唴閮ㄤ綅缃鎸囬拡绉诲埌鏂囦欢棣栥傚啀杩涘叆寰鐜阃愪釜鏄剧ず褰揿墠鏂囦欢涓镄勫叏閮ㄥ唴瀹广

13.4.3 鏁版嵁鍧楄诲啓鍑芥暟fread鍜宖wtrite
C璇瑷杩樻彁渚涗简鐢ㄤ簬鏁村潡鏁版嵁镄勮诲啓鍑芥暟銆傚彲鐢ㄦ潵璇诲啓涓缁勬暟鎹锛屽备竴涓鏁扮粍鍏幂礌锛屼竴涓缁撴瀯鍙橀噺镄勫肩瓑銆

璇绘暟鎹鍧楀嚱鏁拌皟鐢ㄧ殑涓鑸褰㈠纺涓猴细

fread(buffer,size,count,fp);

鍐欐暟鎹鍧楀嚱鏁拌皟鐢ㄧ殑涓鑸褰㈠纺涓猴细

fwrite(buffer,size,count,fp);

鍏朵腑:

buffer 鏄涓涓鎸囬拡锛屽湪fread鍑芥暟涓锛屽畠琛ㄧず瀛樻斁杈揿叆鏁版嵁镄勯栧湴鍧銆傚湪fwrite鍑芥暟涓锛屽畠琛ㄧず瀛樻斁杈揿嚭鏁版嵁镄勯栧湴鍧銆

size 琛ㄧず鏁版嵁鍧楃殑瀛楄妭鏁般

count 琛ㄧず瑕佽诲啓镄勬暟鎹鍧楀潡鏁般

fp 琛ㄧず鏂囦欢鎸囬拡銆

渚嫔傦细

fread(fa,4,5,fp);

鍏舵剰涔夋槸浠巉p镓鎸囩殑鏂囦欢涓锛屾疮娆¤4涓瀛楄妭(涓涓瀹炴暟)阃佸叆瀹炴暟缁刦a涓锛岃繛缁璇5娆★纴鍗宠5涓瀹炴暟鍒癴a涓銆

銆愪緥13.6銆戜粠阌鐩樿緭鍏ヤ袱涓瀛︾敓鏁版嵁锛屽啓鍏ヤ竴涓鏂囦欢涓锛屽啀璇诲嚭杩欎袱涓瀛︾敓镄勬暟鎹鏄剧ず鍦ㄥ睆骞曚笂銆

#include<stdio.h>

struct stu

{

char name[10];

int num;

int age;

char addr[15];

}boya[2],boyb[2],*pp,*qq;

main()

{

FILE *fp;

char ch;

int i;

pp=boya;

qq=boyb;

if((fp=fopen("d:\\jrzh\\example\\stu_list","wb+"))==NULL)

{

printf("Cannot open file strike any key exit!");

getch();

exit(1);

}

printf("\ninput data\n");

for(i=0;i<2;i++,pp++)

scanf("%s%d%d%s",pp->name,&pp->num,&pp->age,pp->addr);

pp=boya;

fwrite(pp,sizeof(struct stu),2,fp);

rewind(fp);

fread(qq,sizeof(struct stu),2,fp);

printf("\n\nname\tnumber age addr\n");

for(i=0;i<2;i++,qq++)

printf("%s\t%5d%7d %s\n",qq->name,qq->num,qq->age,qq->addr);

fclose(fp);

}

链渚嬬▼搴忓畾涔変简涓涓缁撴瀯stu,璇存槑浜嗕袱涓缁撴瀯鏁扮粍boya鍜宐oyb浠ュ强涓や釜缁撴瀯鎸囬拡鍙橀噺pp鍜宷q銆俻p鎸囧悜boya,qq鎸囧悜boyb銆傜▼搴忕16琛屼互璇诲啓鏂瑰纺镓揿紑浜岃繘鍒舵枃浠垛渟tu_list钬濓纴杈揿叆浜屼釜瀛︾敓鏁版嵁涔嫔悗锛屽啓鍏ヨユ枃浠朵腑锛岀劧钖庢妸鏂囦欢鍐呴儴浣岖疆鎸囬拡绉诲埌鏂囦欢棣栵纴璇诲嚭涓ゅ潡瀛︾敓鏁版嵁钖庯纴鍦ㄥ睆骞曚笂鏄剧ず銆

------

锲炲嶆ゆ枃绔 |
锲炲崭富棰:Re:c璇瑷鏂囦欢镎崭綔 | 浣滆: haohao | 鍐涜:鍏绾у啗澹 | 鍙戣〃镞堕棿:2004-07-28 11:08:47
涓嫔崃搴旇ユ妸杩欎釜鎼炲畾浜.
------

锲炲嶆ゆ枃绔 |
锲炲崭富棰:Re:Re:c璇瑷鏂囦欢镎崭綔 | 浣滆: haohao | 鍐涜:鍏绾у啗澹 | 鍙戣〃镞堕棿:2004-07-28 11:10:23
13.7 C搴撴枃浠
C绯荤粺鎻愪緵浜嗕赴瀵岀殑绯荤粺鏂囦欢锛岀О涓哄簱鏂囦欢锛孋镄勫簱鏂囦欢鍒嗕负涓ょ被锛屼竴绫绘槸镓╁𪾢钖崭负".h"镄勬枃浠讹纴绉颁负澶存枃浠讹纴鍦ㄥ墠闱㈢殑鍖呭惈锻戒护涓鎴戜滑宸插氭′娇鐢ㄨ繃銆傚湪".h"鏂囦欢涓鍖呭惈浜嗗父閲忓畾涔夈佺被鍨嫔畾涔夈佸畯瀹氢箟銆佸嚱鏁板师鍨嬩互鍙婂悇绉岖紪璇戦夋嫨璁剧疆绛変俊鎭銆傚彟涓绫绘槸鍑芥暟搴掳纴鍖呮嫭浜嗗悇绉嶅嚱鏁扮殑鐩镙囦唬镰侊纴渚涚敤鎴峰湪绋嫔簭涓璋幂敤銆 阃氩父鍦ㄧ▼搴忎腑璋幂敤涓涓搴揿嚱鏁版椂锛岃佸湪璋幂敤涔嫔墠鍖呭惈璇ュ嚱鏁板师鍨嬫墍鍦ㄧ殑".h" 鏂囦欢銆

涓嬮溃缁椤嚭Turbo C镄勫叏閮".h"鏂囦欢銆

Turbo C澶存枃浠

n ALLOC.H 璇存槑鍐呭瓨绠$悊鍑芥暟(鍒嗛厤銆侀喷鏀剧瓑)銆

n ASSERT.H 瀹氢箟 assert璋冭瘯瀹忋

n BIOS.H 璇存槑璋幂敤IBM钬摈C ROM BIOS瀛愮▼搴忕殑钖勪釜鍑芥暟銆

n CONIO.H 璇存槑璋幂敤DOS鎺у埗鍙癐/O瀛愮▼搴忕殑钖勪釜鍑芥暟銆

n CTYPE.H 鍖呭惈链夊叧瀛楃﹀垎绫诲强杞鎹㈢殑钖岖被淇℃伅(濡 isalpha鍜宼oascii绛)銆

n DIR.H 鍖呭惈链夊叧鐩褰曞拰璺寰勭殑缁撴瀯銆佸畯瀹氢箟鍜屽嚱鏁般

n DOS.H 瀹氢箟鍜岃存槑MSDOS鍜8086璋幂敤镄勪竴浜涘父閲忓拰鍑芥暟銆

n ERRON.H 瀹氢箟阌栾浠g爜镄勫姪璁扮︺

n FCNTL.H 瀹氢箟鍦ㄤ笌open搴揿瓙绋嫔簭杩炴帴镞剁殑绗﹀彿甯搁噺銆

n FLOAT.H 鍖呭惈链夊叧娴镣硅繍绠楃殑涓浜涘弬鏁板拰鍑芥暟銆

n GRAPHICS.H 璇存槑链夊叧锲惧舰锷熻兘镄勫悇涓鍑芥暟锛屽浘褰㈤敊璇浠g爜镄勫父閲忓畾涔夛纴姝e逛笉钖岄┍锷ㄧ▼搴忕殑钖勭嶉滆壊鍊硷纴鍙婂嚱鏁扮敤鍒扮殑涓浜涚壒娈婄粨鏋勚

n IO.H 鍖呭惈浣庣骇I/O瀛愮▼搴忕殑缁撴瀯鍜岃存槑銆

n LIMIT.H 鍖呭惈钖勭幆澧冨弬鏁般佺紪璇戞椂闂撮檺鍒躲佹暟镄勮寖锲寸瓑淇℃伅銆

n MATH.H 璇存槑鏁板﹁繍绠楀嚱鏁帮纴杩桦畾浜 HUGE VAL 瀹忥纴 璇存槑浜唌atherr鍜宫atherr瀛愮▼搴忕敤鍒扮殑鐗规畩缁撴瀯銆

n MEM.H 璇存槑涓浜涘唴瀛樻搷浣滃嚱鏁(鍏朵腑澶у氭暟涔熷湪STRING.H涓璇存槑)銆

n PROCESS.H 璇存槑杩涚▼绠$悊镄勫悇涓鍑芥暟锛宻pawn钬﹀拰EXEC 钬﹀嚱鏁扮殑缁撴瀯璇存槑銆

n SETJMP.H 瀹氢箟longjmp鍜宻etjmp鍑芥暟鐢ㄥ埌镄刯mp buf绫诲瀷锛岃存槑杩欎袱涓鍑芥暟銆

n SHARE.H 瀹氢箟鏂囦欢鍏变韩鍑芥暟镄勫弬鏁般

n SIGNAL.H 瀹氢箟SIG[ZZ(Z] [ZZ)]IGN鍜孲IG[ZZ(Z] [ZZ)]DFL甯搁噺锛岃存槑rajse鍜宻ignal涓や釜鍑芥暟銆

n STDARG.H 瀹氢箟璇诲嚱鏁板弬鏁拌〃镄勫畯銆(濡倂printf,vscarf鍑芥暟)銆

n STDDEF.H 瀹氢箟涓浜涘叕鍏辨暟鎹绫诲瀷鍜屽畯銆

n STDIO.H 瀹氢箟Kernighan鍜孯itchie鍦║nix System V 涓瀹氢箟镄勬爣鍑嗗拰镓╁𪾢镄勭被鍨嫔拰瀹忋傝缮瀹氢箟镙囧嗳I/O 棰勫畾涔夋祦锛歴tdin,stdout鍜宻tderr锛岃存槑 I/O娴佸瓙绋嫔簭銆

n STDLIB.H 璇存槑涓浜涘父鐢ㄧ殑瀛愮▼搴忥细杞鎹㈠瓙绋嫔簭銆佹悳绱/ 鎺掑簭瀛愮▼搴忕瓑銆

n STRING.H 璇存槑涓浜涗覆镎崭綔鍜屽唴瀛樻搷浣滃嚱鏁般

n SYS\STAT.H 瀹氢箟鍦ㄦ墦寮鍜屽垱寤烘枃浠舵椂鐢ㄥ埌镄勪竴浜涚﹀彿甯搁噺銆

n SYS\TYPES.H 璇存槑ftime鍑芥暟鍜宼imeb缁撴瀯銆

n SYS\TIME.H 瀹氢箟镞堕棿镄勭被鍨媡ime[ZZ(Z] [ZZ)]t銆

n TIME.H 瀹氢箟镞堕棿杞鎹㈠瓙绋嫔簭asctime銆乴ocaltime鍜実mtime镄勭粨鏋勶纴ctime銆 difftime銆 gmtime銆 localtime鍜宻time鐢ㄥ埌镄勭被鍨嬶纴骞舵彁渚涜繖浜涘嚱鏁扮殑铡熷瀷銆

n VALUE.H 瀹氢箟涓浜涢吨瑕佸父閲忥纴鍖呮嫭渚濊禆浜庢満鍣ㄧ‖浠剁殑鍜屼负涓嶶nix System V鐩稿吋瀹硅岃存槑镄勪竴浜涘父閲忥纴鍖呮嫭娴镣瑰拰鍙岀簿搴﹀肩殑锣冨洿銆

13.8 链绔犲皬缁
1. C绯荤粺鎶婃枃浠跺綋浣滀竴涓钬沧祦钬濓纴鎸夊瓧鑺傝繘琛屽勭悊銆

2. C鏂囦欢鎸夌紪镰佹柟寮忓垎涓轰簩杩涘埗鏂囦欢鍜孉SCII鏂囦欢銆

3. C璇瑷涓锛岀敤鏂囦欢鎸囬拡镙囱瘑鏂囦欢锛屽綋涓涓鏂囦欢琚镓揿紑镞讹纴鍙鍙栧缑璇ユ枃浠舵寚阍堛

4. 鏂囦欢鍦ㄨ诲啓涔嫔墠蹇呴’镓揿紑锛岃诲啓缁撴潫蹇呴’鍏抽棴銆

5. 鏂囦欢鍙鎸夊彧璇汇佸彧鍐欍佽诲啓銆佽拷锷犲洓绉嶆搷浣沧柟寮忔墦寮锛屽悓镞惰缮蹇呴’鎸囧畾鏂囦欢镄勭被鍨嬫槸浜岃繘鍒舵枃浠惰缮鏄鏂囨湰鏂囦欢銆

6. 鏂囦欢鍙鎸夊瓧鑺傦纴瀛楃︿覆锛屾暟鎹鍧椾负鍗曚綅璇诲啓锛屾枃浠朵篃鍙鎸夋寚瀹氱殑镙煎纺杩涜岃诲啓銆

7. 鏂囦欢鍐呴儴镄勪綅缃鎸囬拡鍙鎸囩ず褰揿墠镄勮诲啓浣岖疆锛岀Щ锷ㄨユ寚阍埚彲浠ュ规枃浠跺疄鐜伴殢链鸿诲啓銆

热点内容
我的世界服务器显示村民名字 发布:2024-11-26 07:37:16 浏览:478
php注册与登录 发布:2024-11-26 07:31:21 浏览:795
基金账户如何配置 发布:2024-11-26 07:29:58 浏览:180
用电脑怎么刷汽车行车电脑配置 发布:2024-11-26 07:24:14 浏览:688
客户端ip和服务器ip地址怎么设置 发布:2024-11-26 07:18:25 浏览:684
如何破解加密的wmv 发布:2024-11-26 07:18:16 浏览:895
资源码项目 发布:2024-11-26 07:10:12 浏览:892
acl配置第三步如何验证 发布:2024-11-26 07:07:58 浏览:939
上传gif搜索 发布:2024-11-26 06:27:05 浏览:763
linux用户组文件 发布:2024-11-26 06:26:58 浏览:89