[C ]Các thao tác trên file.pdf

  • Published on
    14-Dec-2014

  • View
    77

  • Download
    11

Embed Size (px)

Transcript

<p>17/03/2013</p> <p>[C++]Cc thao tc trn fileTm kim Tm kim Ty Chnh Cn cng tc pht trin website</p> <p>HUONG DAN LAP TRINH .COMHng dn lp trnh PHP, Huong dan Lap trinh ASP,Hng dn Lp trnh Ajax,Hng dn Lp trnh JavaScript v.v...</p> <p>Home</p> <p>Danh ngn</p> <p>Trung tm tin hc</p> <p>Mi bn ng nhp -&gt; Click Vit bi ^^^</p> <p>HC LP TRNHAjax - JavaScript Apache ASP - ASP.NET AutoIT HTML - CSS PHP Java SQL - MySQL Visual Basic - VB.NET C - C++ - Visual C#-Pascal</p> <p>[C++]Cc thao tc trn fileCh nht, 24 Thng 8 2008 07:46 rox_rook Hng dn lp trnh C</p> <p>- C++ - Visual C#-Pascal Trong C++ th ta c 3 loi File stream c bn sau : ifstream : Dng cho file nhp vo. Loi ny ch c th c dng c d liu vo file vo b nh m thi. ofstream : Dng cho file xut ra. Loi ny th c th dng to ra files v chp d liu vo chng. fstream : y l knh file.(File stream). Loi ny th c th va to file, va ghi d liu vo file v c d d liu t file vo lun. I.Cch s dng fstream : nh ngha 1 i tng file ta dng c php sau :</p> <p>f s t r e a md a t a F i l e</p> <p> y dataFile ch l tn do ngi dng t m thi. m 1 file ta dng c php sau :</p> <p>d a t a F i l e . o p e n ( " i n f o . t x t " ,i o s : : o u t ) ; Tm kim Tm kim Ty Chnh &gt;T chc s kin 64 Tnh&gt;o</p> <p> y hm open i hi 2 i s : i th nht l 1 chui tn cha tn file. i th 2 l 1 flag ca file v ci ny cho ta bit ch no m chng ta dng m file. v d trn th tn file l info.txt cn flag file y l ios::out. Ci ny n ni cho C++ bit chng ta m file ch xut ra. Ch xut ra cho php d liu c th c ghi vo file.</p> <p>to ha,Chp hnh Gao Dua Hoat Tinh d a t a f i l e . o p e n ( " i n f o . t x t " ,i o s : : i n ) ;</p> <p>&gt;Than &gt;Than &gt;Bao &gt;Ban</p> <p>hanh photocopy va cho thue nha dat bao ve Cn v d ny th tc l ta ang m file ch nhp vo, tc l cho php d liu c c vo t file. Chng ta c 1 s ch c bn sau y : ios::app Ch gn vo. Nu file c to th ni dung ca n s c gn vo tn cng ca file. Theo mc nh th i vi ch ny th nu file cha c to n s to ra 1 file mi. ios::ateNu file c to, th chng trnh s chy ti trc tip ch cui cng ca file. Xut ra c th l c ghi ra bt k ch no trong file. ios::binaryCh nh phn. Khi m file c m ra ch ny th d liu s c c hay ghi t 1 nh dng nguyn thy nh phn. ios::truncNu file c to th ni dung ca n s b xa i. Chng ta cn c th s dng nhng ch trn chung vi nhau v chng s c kt ni vi nhau bng ton t |. V d d a t a F i l e . o p e n ( " i n f o . t x t " ,i o s : : i n|i o s : : o u t ) ;</p> <p>&gt;Tuyen</p> <p>NG NHP VIT BITn ng nhp</p> <p>Mt khu</p> <p>Lu mt khu! NG NH P</p> <p>Bn qun Mt khu? Bn qun Tn ng nhp? ng k</p> <p>Dng lnh trn cho php ta m file info.txt c 2 ch xut v nhp. Ch : Khi dng ring l th ios::out s xa ni dung ca file nu file c to sn. Tuy nhin nu dng chung vi ios::in, th ni dung file c s c gi li. V nu file cha c to, n s to ra 1 file mi cho chng ta lun. By h l 1 v d hon chnh : program 1</p> <p>S ln xem bi vit : 99404587</p> <p>/ /T h i sp r o g r a mu s e sa nf s t r e a mo b j e c tt ow r i t ed a t at oaf i l e .# i n c l u d e&lt; i o s t r e a m &gt;# i n c l u d e&lt; f s t r e a m &gt;u s i n gn a m e s p a c es t d d a t a F i l e . o p e n ( " d e m o f i l e . t x t " ,i o s : : o u t ) ; / /M f i l e g h iv o c o u t&lt; </p>