如题所述
ofstreamæ¯ä»å
åå°ç¡¬çï¼ifstreamæ¯ä»ç¡¬çå°å
åï¼å
¶å®æè°çæµç¼å²å°±æ¯å
å空é´; å¨C++ä¸ï¼æä¸ä¸ªstreamè¿ä¸ªç±»ï¼ææçI/Oé½ä»¥è¿ä¸ªâæµâ类为åºç¡çï¼å
æ¬æ们è¦è®¤è¯çæ件I/Oï¼streamè¿ä¸ªç±»æ两个éè¦çè¿ç®ç¬¦ï¼ 1ãæå
¥å¨(< < )
ããåæµè¾åºæ°æ®ãæ¯å¦è¯´ç³»ç»æä¸ä¸ªé»è®¤çæ åè¾åºæµ(cout)ï¼ä¸è¬æ åµä¸å°±æ¯æçæ¾ç¤ºå¨ï¼æ以ï¼cout< < " Write Stdout" < < '\n'; 就表示æå符串" Write Stdout" åæ¢è¡å符('\n')è¾åºå°æ åè¾åºæµã 2ãæåå¨(> > )
ããä»æµä¸è¾å ¥æ°æ®ãæ¯å¦è¯´ç³»ç»æä¸ä¸ªé»è®¤çæ åè¾å ¥æµ(cin)ï¼ä¸è¬æ åµä¸å°±æ¯æçé®çï¼æ以ï¼cin> > x; 就表示ä»æ åè¾å ¥æµä¸è¯»åä¸ä¸ªæå®ç±»å(å³åéxçç±»å)çæ°æ®ã ããå¨C++ä¸ï¼å¯¹æ件çæä½æ¯éè¿streamçåç±»fstream(file stream)æ¥å®ç°çï¼æ以ï¼è¦ç¨è¿ç§æ¹å¼æä½æ件ï¼å°±å¿ é¡»å å ¥å¤´æ件fstream.hãä¸é¢å°±ææ¤ç±»çæ件æä½è¿ç¨ä¸ä¸éæ¥ã ä¸ãæå¼æ件
ããå¨fstreamç±»ä¸ï¼æä¸ä¸ªæåå½æ°open()ï¼å°±æ¯ç¨æ¥æå¼æ件çï¼å ¶ååæ¯ï¼ void open(const char* filename,int mode,int access); åæ°ï¼ filenameï¼ããè¦æå¼çæ件å
modeï¼ããããè¦æå¼æ件çæ¹å¼
accessï¼ãããæå¼æ件çå±æ§
æå¼æ件çæ¹å¼å¨ç±»ios(æ¯æææµå¼I/Oç±»çåºç±»)ä¸å®ä¹ï¼å¸¸ç¨çå¼å¦ä¸ï¼ ios::appï¼ããã以追å çæ¹å¼æå¼æ件
ios::ateï¼ãããæ件æå¼åå®ä½å°æ件尾ï¼ios:appå°±å å«ææ¤å±æ§
ios::binaryï¼ ã以äºè¿å¶æ¹å¼æå¼æ件ï¼ç¼ºççæ¹å¼æ¯ææ¬æ¹å¼ã两ç§æ¹å¼çåºå«è§åæ
ios::inï¼ããã æ件以è¾å ¥æ¹å¼æå¼ï¼æ件æ°æ®è¾å ¥å°å åï¼
ios::outï¼ãããæ件以è¾åºæ¹å¼æå¼ï¼å åæ°æ®è¾åºå°æ件ï¼
ios::nocreateï¼ ä¸å»ºç«æ件ï¼æ以æ件ä¸åå¨æ¶æå¼å¤±è´¥
ios::noreplaceï¼ä¸è¦çæ件ï¼æ以æå¼æ件æ¶å¦ææ件åå¨å¤±è´¥
ios::truncï¼ããå¦ææ件åå¨ï¼ææ件é¿åº¦è®¾ä¸º0
ããå¯ä»¥ç¨âæâæ以ä¸å±æ§è¿æ¥èµ·æ¥ï¼å¦ios::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è¿æåopen()ä¸æ ·çæé å½æ°ï¼å¯¹äºä¸ä¾ï¼å¨å®ä¹çæ¶ä¾¯å°±å¯ä»¥æå¼æ件äºï¼
fstream file1(" c:\\config.sys" ); ããç¹å«æåºçæ¯ï¼fstreamæ两个åç±»ï¼ifstream(input file stream)åofstream(outpu file stream)ï¼ifstreamé»è®¤ä»¥è¾å ¥æ¹å¼æå¼æ件ï¼èofstreamé»è®¤ä»¥è¾åºæ¹å¼æå¼æ件ã
ifstream file2(" c:\\pdos.def" ); //以è¾å ¥æ¹å¼æå¼æ件
ofstream file3(" c:\\x.123" ); //以è¾åºæ¹å¼æå¼æ件 ããæ以ï¼å¨å®é åºç¨ä¸ï¼æ ¹æ®éè¦çä¸åï¼éæ©ä¸åçç±»æ¥å®ä¹ï¼å¦ææ³ä»¥è¾å ¥æ¹å¼æå¼ï¼å°±ç¨ifstreamæ¥å®ä¹ï¼å¦ææ³ä»¥è¾åºæ¹å¼æå¼ï¼å°±ç¨ofstreamæ¥å®ä¹ï¼å¦ææ³ä»¥è¾å ¥/è¾åºæ¹å¼æ¥æå¼ï¼å°±ç¨fstreamæ¥å®ä¹ã äºãå ³éæ件
ããæå¼çæ件使ç¨å®æåä¸å®è¦å ³éï¼fstreamæä¾äºæåå½æ°close()æ¥å®ææ¤æä½ï¼å¦ï¼file1.close(); å°±æfile1ç¸è¿çæä»¶å ³éã ä¸ã读åæ件
ãã读åæ件å为ææ¬æ件åäºè¿å¶æ件ç读åï¼å¯¹äºææ¬æ件ç读åæ¯è¾ç®åï¼ç¨æå ¥å¨åæåå¨å°±å¯ä»¥äºï¼è对äºäºè¿å¶ç读åå°±è¦å¤æäºï¼ä¸è¦å°±è¯¦ç»çä»ç»è¿ä¸¤ç§æ¹å¼ ãã1ãææ¬æ件ç读å
ããææ¬æ件ç读åå¾ç®åï¼ç¨æå ¥å¨(< < )åæ件è¾åºï¼ç¨æåå¨(> > )ä»æ件è¾å ¥ãå设file1æ¯ä»¥è¾å ¥æ¹å¼æå¼ï¼file2以è¾åºæå¼ã示ä¾å¦ä¸ï¼ ããfile2< < " I Love You" ; //åæ件åå ¥å符串" I Love You"
ããint i;
ããfile1> > i; //ä»æ件è¾å ¥ä¸ä¸ªæ´æ°å¼ã ããè¿ç§æ¹å¼è¿æä¸ç§ç®åçæ ¼å¼åè½åï¼æ¯å¦å¯ä»¥æå®è¾åºä¸º16è¿å¶ççï¼å ·ä½çæ ¼å¼æ以ä¸ä¸äº æ纵符 åè½ è¾å ¥/è¾åº
dec æ ¼å¼å为åè¿å¶æ°å¼æ°æ® è¾å ¥åè¾åº
endl è¾åºä¸ä¸ªæ¢è¡ç¬¦å¹¶å·æ°æ¤æµ è¾åº
ends è¾åºä¸ä¸ªç©ºå符 è¾åº
hex æ ¼å¼å为åå è¿å¶æ°å¼æ°æ® è¾å ¥åè¾åº
oct æ ¼å¼åä¸ºå «è¿å¶æ°å¼æ°æ® è¾å ¥åè¾åº
setpxecision(int p) 设置浮ç¹æ°ç精度ä½æ° è¾åº ããæ¯å¦è¦æ123å½ä½åå è¿å¶è¾åºï¼file1< < hex< < 123; è¦æ3.1415926以5ä½ç²¾åº¦è¾åºï¼file1< < setpxecision(5)< < 3.1415926ã ãã2ãäºè¿å¶æ件ç读å
â put()
ããput()å½æ°åæµåå ¥ä¸ä¸ªå符ï¼å ¶ååæ¯ofstream &put(char ch)ï¼ä½¿ç¨ä¹æ¯è¾ç®åï¼å¦file1.put('c'); å°±æ¯åæµåä¸ä¸ªå符'c'ã â¡get()
ããget()å½æ°æ¯è¾çµæ´»ï¼æ3ç§å¸¸ç¨çé载形å¼ï¼ ããä¸ç§å°±æ¯åput()对åºçå½¢å¼ï¼ifstream &get(char & ch); åè½æ¯ä»æµä¸è¯»åä¸ä¸ªå符ï¼ç»æä¿åå¨å¼ç¨chä¸ï¼å¦æå°æ件尾ï¼è¿å空å符ãå¦file2.get(x); 表示ä»æ件ä¸è¯»åä¸ä¸ªå符ï¼å¹¶æ读åçå符ä¿åå¨xä¸ã ããå¦ä¸ç§é载形å¼çååæ¯ï¼ int get(); è¿ç§å½¢å¼æ¯ä»æµä¸è¿åä¸ä¸ªå符ï¼å¦æå°è¾¾æ件尾ï¼è¿åEOFï¼å¦x=file2.get(); åä¸ä¾åè½æ¯ä¸æ ·çã ããè¿æä¸ç§å½¢å¼çååæ¯ï¼ifstream &get(char *buf,int num,char delim='\n')ï¼è¿ç§å½¢å¼æåç¬¦è¯»å ¥ç± buf æåçæ°ç»ï¼ç´å°è¯»å ¥äº num 个å符æéå°äºç± delim æå®çå符ï¼å¦ææ²¡ä½¿ç¨ delim è¿ä¸ªåæ°ï¼å°ä½¿ç¨ç¼ºçå¼æ¢è¡ç¬¦'\n'ãä¾å¦ï¼ ããfile2.get(str1,127,'A'); //ä»æ件ä¸è¯»åå符å°å符串str1ï¼å½éå°å符'A'æ读åäº127个å符æ¶ç»æ¢ã â¢è¯»åæ°æ®å
ããè¦è¯»åäºè¿å¶æ°æ®åï¼ä½¿ç¨æåå½æ°read()åwrite()æåå½æ°ï¼å®ä»¬ååå¦ä¸ï¼ ããããread(unsigned char *buf,int num);
ããããwrite(const unsigned char *buf,int num); ããread()ä»æ件ä¸è¯»å num 个åç¬¦å° buf æåçç¼åä¸ï¼å¦æå¨è¿æªè¯»å ¥ num 个å符æ¶å°±å°äºæ件尾ï¼å¯ä»¥ç¨æåå½æ° int gcount(); æ¥åå¾å®é 读åçå符æ°ï¼è write() ä»buf æåçç¼åå 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)); //ä»xxx.xxxä¸è¯»åæå®ä¸ªæ´æ°ï¼æ³¨æç±»å转æ¢
ããããin.close(); out.close(); åãæ£æµEOF
ããæåå½æ°eof()ç¨æ¥æ£æµæ¯å¦å°è¾¾æ件尾ï¼å¦æå°è¾¾æ件尾è¿åé0å¼ï¼å¦åè¿å0ãååæ¯int eof(); ä¾ï¼ããif(in.eof()) ShowMessage(" å·²ç»å°è¾¾æ件尾ï¼" ); äºãæ件å®ä½
ããåCçæ件æä½æ¹å¼ä¸åçæ¯ï¼C++ I/Oç³»ç»ç®¡ç两个ä¸ä¸ä¸ªæ件ç¸èç³»çæéãä¸ä¸ªæ¯è¯»æéï¼å®è¯´æè¾å ¥æä½å¨æ件ä¸çä½ç½®ï¼å¦ä¸ä¸ªæ¯åæéï¼å®ä¸æ¬¡åæä½çä½ç½®ãæ¯æ¬¡æ§è¡è¾å ¥æè¾åºæ¶ï¼ç¸åºçæéèªå¨ååãæ以ï¼C++çæ件å®ä½å为读ä½ç½®ååä½ç½®çå®ä½ï¼å¯¹åºçæåå½æ°æ¯seekg()åseekp()ãseekg()æ¯è®¾ç½®è¯»ä½ç½®ï¼seekpæ¯è®¾ç½®åä½ç½®ãå®ä»¬æéç¨çå½¢å¼å¦ä¸ï¼ ãããã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ï¼ããæ件ç»å°¾ ããè¿ä¸¤ä¸ªå½æ°ä¸è¬ç¨äºäºè¿å¶æ件ï¼å 为ææ¬æ件ä¼å 为系ç»å¯¹å符ç解éèå¯è½ä¸é¢æ³çå¼ä¸åãä¾ï¼ ãããfile1.seekg(1234,ios::cur); //ææ件ç读æéä»å½åä½ç½®åå移1234个åè
ãããfile2.seekp(1234,ios::beg); //ææ件çåæéä»æ件å¼å¤´åå移1234个åè
C++æ ååº ä¹ iostreamåºçå¦ä¹ ç¬è®°ï¼äºï¼fstreamåºä»¥åofstreamç±»çä½¿ç¨ iostreamåºä¸ä» æ¯æç»ç«¯è®¾å¤çè¾å ¥è¾åºï¼è¿æ¯ææ件çè¾å ¥è¾åºï¼åæ件æå ³çè¾å ¥è¾åºç±»å£°æå¨fstream头æ件ä¸ï¼æä¸ä¸ªç±»è´è´£æ件çè¾å ¥è¾åº
1) ifstreamç±»:ä»istream类派çã
2) ofstreamç±»:ä»ostream类派çã
3) fstreamç±»:ä»iostream类派çã
ç±äºæ件çè¾å ¥è¾åºåé®çé¼ æ çè¾å ¥è¾åºæ¯ä¸ä¸æ ·çï¼ä¸è¬pcæºåªæä¸ä¸ªé®ç设å¤ï¼æ以iostreamåºå é¨å£°æäºä¸ä¸ªistreamç±»ç对象cinï¼è¿ä¸ªå¯¹è±¡è´è´£ä»é®çè·åæ°æ®ï¼èæ件设å¤å¨ç³»ç»ä¸æ¯ç±è®¸å¤çï¼æ以iostreamåºå é¨æ æ³ç»ä½ 为æºå¨çæ¯ä¸ªæ件é½å建ä¸ä¸ªè´è´£è·åæ°æ®çifstream对象åè´è´£åå ¥æ°æ®çofstream对象ï¼æ以æ们è¦é对ä¸ä¸ªæ件è¿è¡è¯»åæåå ¥æ°æ®çæ¶åé½è¦èªå·±å建ä¸ä¸ªifstreamæostreamç±»ç对象æ¥ç¨ãofstreamç±»çé»è®¤æé å½æ°å¦ä¸:ofstream::ofstream(const char* filename, int mode = ios::out, int openport = filebuf::openport); filenameæ¯è¦æå¼çæ件åï¼
modeæ¯æå¼çæ¹å¼ï¼
openportæ¯æå¼æ件çå±æ§ã
modeå¯ä»¥è®¾ç½®çæ¹å¼å¦ä¸ï¼
ios::app 以追å çæ¹å¼æå¼
ios::ate æ件æå¼åå®ä½å°æ件尾
ios::binary 以äºè¿å¶æ¹å¼æå¼æ件ï¼é»è®¤æ¯ä»¥ææ¬æ¹å¼æå¼
ios::in æ件以读(è¾å ¥)æ¹å¼æå¼
ios::out æ件以å(è¾åº)æ¹å¼æå¼
ios::trunc å¦ææ件åå¨ï¼åææä»¶æ¸ ç©ºã
以ä¸å±æ§ç¨â|â(æä½æ)è¿æ¥èµ·æ¥ã
openprotå±æ§å¦ä¸ï¼
0 æ®éæ件
1 åªè¯»æ件
2 éå«æ件
4 ç³»ç»æ件
以ä¸å±æ§å¯ä»¥ç¨å æè æä½ææ¹å¼ç»ç»èµ·æ¥ï¼æ¯å¦1|2å3é½ä»£è¡¨æ¢æ¯åªè¯»åæ¯éå«æ件ã
å¨windowsæä½ç³»ç»ä¸å¯ä»¥ä¸è¦ç¬¬ä¸ä¸ªåæ°ï¼å¦æå å ¥ç¬¬ä¸ä¸ªåæ°ï¼é£ç¬¬ä¸ä¸ªåæ°æ¯æå¼æ件çå ±äº«æ¹å¼ï¼ä¹å°±æ¯æå¼è¿ä¸ªæ件æ¶ï¼å ¶ä»è¿ç¨æ¯å¦å¯ä»¥è¯»å该æ件ã
å ±äº«æ¹å¼åæ°å¯ä»¥æ¯ä¸é¢çå¼ï¼
0x10 //_SH_DENYRW Denies read and write access to the file
0x20 //_SH_DENYWR Denies write access to the file
0x30 //_SH_DENYRD Denies read access to the file.
0x40 //_SH_DENYNO Permits read and write access
å ¶ä»å¼é½ä¼æ¥ "Invalid sharing flag "çé误ã ofstream hFile("c:\\1.txt", ios::out, _SH_DENYRW); // _SH_DENYRW is deny read and write
if (!hFile) // if the file could open, hFile is a handle, else is zero
{
cout << "write fail!" << endl;
cout << "access is denies,maybe the file is readonlys,or use deny read opened of other process." << endl;
}
else
{
hFile << "by coderlee writes";
cout << "write success!" << endl;
}
hFile.close(); // opened file need close.
ä¸é¢æ¯åæ件çäºä¾ä»£ç ï¼å æå¼æ件ï¼ç¶åå¤ææ¯ä¸æ¯0ï¼å¦ææ¯0ï¼åæ示write failå¦ååæ件ï¼æ示write success.
ããåæµè¾åºæ°æ®ãæ¯å¦è¯´ç³»ç»æä¸ä¸ªé»è®¤çæ åè¾åºæµ(cout)ï¼ä¸è¬æ åµä¸å°±æ¯æçæ¾ç¤ºå¨ï¼æ以ï¼cout< < " Write Stdout" < < '\n'; 就表示æå符串" Write Stdout" åæ¢è¡å符('\n')è¾åºå°æ åè¾åºæµã 2ãæåå¨(> > )
ããä»æµä¸è¾å ¥æ°æ®ãæ¯å¦è¯´ç³»ç»æä¸ä¸ªé»è®¤çæ åè¾å ¥æµ(cin)ï¼ä¸è¬æ åµä¸å°±æ¯æçé®çï¼æ以ï¼cin> > x; 就表示ä»æ åè¾å ¥æµä¸è¯»åä¸ä¸ªæå®ç±»å(å³åéxçç±»å)çæ°æ®ã ããå¨C++ä¸ï¼å¯¹æ件çæä½æ¯éè¿streamçåç±»fstream(file stream)æ¥å®ç°çï¼æ以ï¼è¦ç¨è¿ç§æ¹å¼æä½æ件ï¼å°±å¿ é¡»å å ¥å¤´æ件fstream.hãä¸é¢å°±ææ¤ç±»çæ件æä½è¿ç¨ä¸ä¸éæ¥ã ä¸ãæå¼æ件
ããå¨fstreamç±»ä¸ï¼æä¸ä¸ªæåå½æ°open()ï¼å°±æ¯ç¨æ¥æå¼æ件çï¼å ¶ååæ¯ï¼ void open(const char* filename,int mode,int access); åæ°ï¼ filenameï¼ããè¦æå¼çæ件å
modeï¼ããããè¦æå¼æ件çæ¹å¼
accessï¼ãããæå¼æ件çå±æ§
æå¼æ件çæ¹å¼å¨ç±»ios(æ¯æææµå¼I/Oç±»çåºç±»)ä¸å®ä¹ï¼å¸¸ç¨çå¼å¦ä¸ï¼ ios::appï¼ããã以追å çæ¹å¼æå¼æ件
ios::ateï¼ãããæ件æå¼åå®ä½å°æ件尾ï¼ios:appå°±å å«ææ¤å±æ§
ios::binaryï¼ ã以äºè¿å¶æ¹å¼æå¼æ件ï¼ç¼ºççæ¹å¼æ¯ææ¬æ¹å¼ã两ç§æ¹å¼çåºå«è§åæ
ios::inï¼ããã æ件以è¾å ¥æ¹å¼æå¼ï¼æ件æ°æ®è¾å ¥å°å åï¼
ios::outï¼ãããæ件以è¾åºæ¹å¼æå¼ï¼å åæ°æ®è¾åºå°æ件ï¼
ios::nocreateï¼ ä¸å»ºç«æ件ï¼æ以æ件ä¸åå¨æ¶æå¼å¤±è´¥
ios::noreplaceï¼ä¸è¦çæ件ï¼æ以æå¼æ件æ¶å¦ææ件åå¨å¤±è´¥
ios::truncï¼ããå¦ææ件åå¨ï¼ææ件é¿åº¦è®¾ä¸º0
ããå¯ä»¥ç¨âæâæ以ä¸å±æ§è¿æ¥èµ·æ¥ï¼å¦ios::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è¿æåopen()ä¸æ ·çæé å½æ°ï¼å¯¹äºä¸ä¾ï¼å¨å®ä¹çæ¶ä¾¯å°±å¯ä»¥æå¼æ件äºï¼
fstream file1(" c:\\config.sys" ); ããç¹å«æåºçæ¯ï¼fstreamæ两个åç±»ï¼ifstream(input file stream)åofstream(outpu file stream)ï¼ifstreamé»è®¤ä»¥è¾å ¥æ¹å¼æå¼æ件ï¼èofstreamé»è®¤ä»¥è¾åºæ¹å¼æå¼æ件ã
ifstream file2(" c:\\pdos.def" ); //以è¾å ¥æ¹å¼æå¼æ件
ofstream file3(" c:\\x.123" ); //以è¾åºæ¹å¼æå¼æ件 ããæ以ï¼å¨å®é åºç¨ä¸ï¼æ ¹æ®éè¦çä¸åï¼éæ©ä¸åçç±»æ¥å®ä¹ï¼å¦ææ³ä»¥è¾å ¥æ¹å¼æå¼ï¼å°±ç¨ifstreamæ¥å®ä¹ï¼å¦ææ³ä»¥è¾åºæ¹å¼æå¼ï¼å°±ç¨ofstreamæ¥å®ä¹ï¼å¦ææ³ä»¥è¾å ¥/è¾åºæ¹å¼æ¥æå¼ï¼å°±ç¨fstreamæ¥å®ä¹ã äºãå ³éæ件
ããæå¼çæ件使ç¨å®æåä¸å®è¦å ³éï¼fstreamæä¾äºæåå½æ°close()æ¥å®ææ¤æä½ï¼å¦ï¼file1.close(); å°±æfile1ç¸è¿çæä»¶å ³éã ä¸ã读åæ件
ãã读åæ件å为ææ¬æ件åäºè¿å¶æ件ç读åï¼å¯¹äºææ¬æ件ç读åæ¯è¾ç®åï¼ç¨æå ¥å¨åæåå¨å°±å¯ä»¥äºï¼è对äºäºè¿å¶ç读åå°±è¦å¤æäºï¼ä¸è¦å°±è¯¦ç»çä»ç»è¿ä¸¤ç§æ¹å¼ ãã1ãææ¬æ件ç读å
ããææ¬æ件ç读åå¾ç®åï¼ç¨æå ¥å¨(< < )åæ件è¾åºï¼ç¨æåå¨(> > )ä»æ件è¾å ¥ãå设file1æ¯ä»¥è¾å ¥æ¹å¼æå¼ï¼file2以è¾åºæå¼ã示ä¾å¦ä¸ï¼ ããfile2< < " I Love You" ; //åæ件åå ¥å符串" I Love You"
ããint i;
ããfile1> > i; //ä»æ件è¾å ¥ä¸ä¸ªæ´æ°å¼ã ããè¿ç§æ¹å¼è¿æä¸ç§ç®åçæ ¼å¼åè½åï¼æ¯å¦å¯ä»¥æå®è¾åºä¸º16è¿å¶ççï¼å ·ä½çæ ¼å¼æ以ä¸ä¸äº æ纵符 åè½ è¾å ¥/è¾åº
dec æ ¼å¼å为åè¿å¶æ°å¼æ°æ® è¾å ¥åè¾åº
endl è¾åºä¸ä¸ªæ¢è¡ç¬¦å¹¶å·æ°æ¤æµ è¾åº
ends è¾åºä¸ä¸ªç©ºå符 è¾åº
hex æ ¼å¼å为åå è¿å¶æ°å¼æ°æ® è¾å ¥åè¾åº
oct æ ¼å¼åä¸ºå «è¿å¶æ°å¼æ°æ® è¾å ¥åè¾åº
setpxecision(int p) 设置浮ç¹æ°ç精度ä½æ° è¾åº ããæ¯å¦è¦æ123å½ä½åå è¿å¶è¾åºï¼file1< < hex< < 123; è¦æ3.1415926以5ä½ç²¾åº¦è¾åºï¼file1< < setpxecision(5)< < 3.1415926ã ãã2ãäºè¿å¶æ件ç读å
â put()
ããput()å½æ°åæµåå ¥ä¸ä¸ªå符ï¼å ¶ååæ¯ofstream &put(char ch)ï¼ä½¿ç¨ä¹æ¯è¾ç®åï¼å¦file1.put('c'); å°±æ¯åæµåä¸ä¸ªå符'c'ã â¡get()
ããget()å½æ°æ¯è¾çµæ´»ï¼æ3ç§å¸¸ç¨çé载形å¼ï¼ ããä¸ç§å°±æ¯åput()对åºçå½¢å¼ï¼ifstream &get(char & ch); åè½æ¯ä»æµä¸è¯»åä¸ä¸ªå符ï¼ç»æä¿åå¨å¼ç¨chä¸ï¼å¦æå°æ件尾ï¼è¿å空å符ãå¦file2.get(x); 表示ä»æ件ä¸è¯»åä¸ä¸ªå符ï¼å¹¶æ读åçå符ä¿åå¨xä¸ã ããå¦ä¸ç§é载形å¼çååæ¯ï¼ int get(); è¿ç§å½¢å¼æ¯ä»æµä¸è¿åä¸ä¸ªå符ï¼å¦æå°è¾¾æ件尾ï¼è¿åEOFï¼å¦x=file2.get(); åä¸ä¾åè½æ¯ä¸æ ·çã ããè¿æä¸ç§å½¢å¼çååæ¯ï¼ifstream &get(char *buf,int num,char delim='\n')ï¼è¿ç§å½¢å¼æåç¬¦è¯»å ¥ç± buf æåçæ°ç»ï¼ç´å°è¯»å ¥äº num 个å符æéå°äºç± delim æå®çå符ï¼å¦ææ²¡ä½¿ç¨ delim è¿ä¸ªåæ°ï¼å°ä½¿ç¨ç¼ºçå¼æ¢è¡ç¬¦'\n'ãä¾å¦ï¼ ããfile2.get(str1,127,'A'); //ä»æ件ä¸è¯»åå符å°å符串str1ï¼å½éå°å符'A'æ读åäº127个å符æ¶ç»æ¢ã â¢è¯»åæ°æ®å
ããè¦è¯»åäºè¿å¶æ°æ®åï¼ä½¿ç¨æåå½æ°read()åwrite()æåå½æ°ï¼å®ä»¬ååå¦ä¸ï¼ ããããread(unsigned char *buf,int num);
ããããwrite(const unsigned char *buf,int num); ããread()ä»æ件ä¸è¯»å num 个åç¬¦å° buf æåçç¼åä¸ï¼å¦æå¨è¿æªè¯»å ¥ num 个å符æ¶å°±å°äºæ件尾ï¼å¯ä»¥ç¨æåå½æ° int gcount(); æ¥åå¾å®é 读åçå符æ°ï¼è write() ä»buf æåçç¼åå 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)); //ä»xxx.xxxä¸è¯»åæå®ä¸ªæ´æ°ï¼æ³¨æç±»å转æ¢
ããããin.close(); out.close(); åãæ£æµEOF
ããæåå½æ°eof()ç¨æ¥æ£æµæ¯å¦å°è¾¾æ件尾ï¼å¦æå°è¾¾æ件尾è¿åé0å¼ï¼å¦åè¿å0ãååæ¯int eof(); ä¾ï¼ããif(in.eof()) ShowMessage(" å·²ç»å°è¾¾æ件尾ï¼" ); äºãæ件å®ä½
ããåCçæ件æä½æ¹å¼ä¸åçæ¯ï¼C++ I/Oç³»ç»ç®¡ç两个ä¸ä¸ä¸ªæ件ç¸èç³»çæéãä¸ä¸ªæ¯è¯»æéï¼å®è¯´æè¾å ¥æä½å¨æ件ä¸çä½ç½®ï¼å¦ä¸ä¸ªæ¯åæéï¼å®ä¸æ¬¡åæä½çä½ç½®ãæ¯æ¬¡æ§è¡è¾å ¥æè¾åºæ¶ï¼ç¸åºçæéèªå¨ååãæ以ï¼C++çæ件å®ä½å为读ä½ç½®ååä½ç½®çå®ä½ï¼å¯¹åºçæåå½æ°æ¯seekg()åseekp()ãseekg()æ¯è®¾ç½®è¯»ä½ç½®ï¼seekpæ¯è®¾ç½®åä½ç½®ãå®ä»¬æéç¨çå½¢å¼å¦ä¸ï¼ ãããã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ï¼ããæ件ç»å°¾ ããè¿ä¸¤ä¸ªå½æ°ä¸è¬ç¨äºäºè¿å¶æ件ï¼å 为ææ¬æ件ä¼å 为系ç»å¯¹å符ç解éèå¯è½ä¸é¢æ³çå¼ä¸åãä¾ï¼ ãããfile1.seekg(1234,ios::cur); //ææ件ç读æéä»å½åä½ç½®åå移1234个åè
ãããfile2.seekp(1234,ios::beg); //ææ件çåæéä»æ件å¼å¤´åå移1234个åè
C++æ ååº ä¹ iostreamåºçå¦ä¹ ç¬è®°ï¼äºï¼fstreamåºä»¥åofstreamç±»çä½¿ç¨ iostreamåºä¸ä» æ¯æç»ç«¯è®¾å¤çè¾å ¥è¾åºï¼è¿æ¯ææ件çè¾å ¥è¾åºï¼åæ件æå ³çè¾å ¥è¾åºç±»å£°æå¨fstream头æ件ä¸ï¼æä¸ä¸ªç±»è´è´£æ件çè¾å ¥è¾åº
1) ifstreamç±»:ä»istream类派çã
2) ofstreamç±»:ä»ostream类派çã
3) fstreamç±»:ä»iostream类派çã
ç±äºæ件çè¾å ¥è¾åºåé®çé¼ æ çè¾å ¥è¾åºæ¯ä¸ä¸æ ·çï¼ä¸è¬pcæºåªæä¸ä¸ªé®ç设å¤ï¼æ以iostreamåºå é¨å£°æäºä¸ä¸ªistreamç±»ç对象cinï¼è¿ä¸ªå¯¹è±¡è´è´£ä»é®çè·åæ°æ®ï¼èæ件设å¤å¨ç³»ç»ä¸æ¯ç±è®¸å¤çï¼æ以iostreamåºå é¨æ æ³ç»ä½ 为æºå¨çæ¯ä¸ªæ件é½å建ä¸ä¸ªè´è´£è·åæ°æ®çifstream对象åè´è´£åå ¥æ°æ®çofstream对象ï¼æ以æ们è¦é对ä¸ä¸ªæ件è¿è¡è¯»åæåå ¥æ°æ®çæ¶åé½è¦èªå·±å建ä¸ä¸ªifstreamæostreamç±»ç对象æ¥ç¨ãofstreamç±»çé»è®¤æé å½æ°å¦ä¸:ofstream::ofstream(const char* filename, int mode = ios::out, int openport = filebuf::openport); filenameæ¯è¦æå¼çæ件åï¼
modeæ¯æå¼çæ¹å¼ï¼
openportæ¯æå¼æ件çå±æ§ã
modeå¯ä»¥è®¾ç½®çæ¹å¼å¦ä¸ï¼
ios::app 以追å çæ¹å¼æå¼
ios::ate æ件æå¼åå®ä½å°æ件尾
ios::binary 以äºè¿å¶æ¹å¼æå¼æ件ï¼é»è®¤æ¯ä»¥ææ¬æ¹å¼æå¼
ios::in æ件以读(è¾å ¥)æ¹å¼æå¼
ios::out æ件以å(è¾åº)æ¹å¼æå¼
ios::trunc å¦ææ件åå¨ï¼åææä»¶æ¸ ç©ºã
以ä¸å±æ§ç¨â|â(æä½æ)è¿æ¥èµ·æ¥ã
openprotå±æ§å¦ä¸ï¼
0 æ®éæ件
1 åªè¯»æ件
2 éå«æ件
4 ç³»ç»æ件
以ä¸å±æ§å¯ä»¥ç¨å æè æä½ææ¹å¼ç»ç»èµ·æ¥ï¼æ¯å¦1|2å3é½ä»£è¡¨æ¢æ¯åªè¯»åæ¯éå«æ件ã
å¨windowsæä½ç³»ç»ä¸å¯ä»¥ä¸è¦ç¬¬ä¸ä¸ªåæ°ï¼å¦æå å ¥ç¬¬ä¸ä¸ªåæ°ï¼é£ç¬¬ä¸ä¸ªåæ°æ¯æå¼æ件çå ±äº«æ¹å¼ï¼ä¹å°±æ¯æå¼è¿ä¸ªæ件æ¶ï¼å ¶ä»è¿ç¨æ¯å¦å¯ä»¥è¯»å该æ件ã
å ±äº«æ¹å¼åæ°å¯ä»¥æ¯ä¸é¢çå¼ï¼
0x10 //_SH_DENYRW Denies read and write access to the file
0x20 //_SH_DENYWR Denies write access to the file
0x30 //_SH_DENYRD Denies read access to the file.
0x40 //_SH_DENYNO Permits read and write access
å ¶ä»å¼é½ä¼æ¥ "Invalid sharing flag "çé误ã ofstream hFile("c:\\1.txt", ios::out, _SH_DENYRW); // _SH_DENYRW is deny read and write
if (!hFile) // if the file could open, hFile is a handle, else is zero
{
cout << "write fail!" << endl;
cout << "access is denies,maybe the file is readonlys,or use deny read opened of other process." << endl;
}
else
{
hFile << "by coderlee writes";
cout << "write success!" << endl;
}
hFile.close(); // opened file need close.
ä¸é¢æ¯åæ件çäºä¾ä»£ç ï¼å æå¼æ件ï¼ç¶åå¤ææ¯ä¸æ¯0ï¼å¦ææ¯0ï¼åæ示write failå¦ååæ件ï¼æ示write success.
温馨提示:答案为网友推荐,仅供参考
第1个回答 2013-05-18
CFILE