资源描述:
《c创建一个文本文件》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C++创建一个文本文件Howtocreateatextdocumentinc++inadiskUsefstreamflowThecommonmethodsforfilesareasfollows:Thisclassinc++,thereisastream,alltheI/Oonthebasisofthe〃flow〃type,includingfileI/Owewanttoknow,stream,thisclasshastwoimportantoperator:Insert(<<<)Outputdat
2、a,tothestream.Forexample,thesystemhasadefaultstandardoutputstream(cout),whichisusuallyreferredtoasadisplay,socout<<<<<;lhestring"WriteStdout"andthenewlinecharacter('n')arerepresentedasoutputtothestandsrdoutputstream・2,thedischer(>,>)Enterdatafromtheflo
3、w・Forexample,thesystemhasadefaultstandardinputstream(cin),whichisusuallyreferredtoasthekeyboard,so,cin>>x;Thismeansthatthedatafromthestandardinputstreamisreadfromaspecifiedtype(thatis,thetypeofvariablex).Inc++,theoperationofthefileisaccomplishedthrough
4、thestream,ssubclassfstream(filestream),sotomanipulatethefileinthisway,youmustaddtheheaderfilefstream.h.Here,showtodothis・OpenthefileInthefstrearnclass,thereisamemberfunctionopen(),whichisusedtoopenthefile,andtheprototypeis:Voidopen(constchar*filename,i
5、ntmode,intaccess);Parameters:Filename:thefilenametoopenMode:thewaytoopenafileAccess:opensthepropertiesofthefileThewaytoopenthefileisdefinedintheclassios(thebaseclassforallthestreamingI/Oclasses),andtheusualvaluesareasfollows:Ios::app:openfilesinadditio
6、nalwaysAte:thefileisopentotheendofthefile,ios:theappcontainsthispropertyBinary:openthefileinbinarymodeandthedefaultmodeistext・ThedifferencebetweenthetwoapproachesisthepreviousoneIn:thefileisopenedinaninputmodeIos::out:thefileopensinanoutputmodeNocreate
7、:nofileiscreated,sofailureisopenwhenthefiledoesn,texistNoreplace:nofile,soifthefilefailswhenyouopenthefileTrunc:ifthefileexists,setthefilelengthto0Youcanusetoconnecttheaboveproperties,likeios::out
8、ios::binaryThepropertiesoftheopenfileare:0:1:commonfile
9、s,openaccesstheread-onlyfile2:thehiddenfiles4:systemfilesYoucanuseeitherortoconnecttheseproperties,suchas3or1
10、2,toopenthefilewithread-onlyandimpliedproperties・Forexample,openfilec:config・sysinbinaryinputFstreamfile1.ios::in,0);File1.Ope