Posted on 2007-08-06 01:09
謝迎飛 閱讀(1134)
評(píng)論(5) 編輯 收藏 引用
前幾天同事遇到一個(gè)小問(wèn)題,他想編個(gè)小工具,主要用來(lái)在每個(gè).cpp文件的開(kāi)始加入一句公共的#include "",于是,感覺(jué)自己在C++這里好像也是個(gè)盲點(diǎn),所以就幫忙一起想想,同時(shí)也有了一個(gè)想法就是可以借此系統(tǒng)的對(duì)C++和MFC,或者更多的類(lèi)似問(wèn)題和關(guān)于文件輸入和輸出的問(wèn)題做個(gè)透徹的認(rèn)識(shí),同時(shí)也是想利用網(wǎng)絡(luò),來(lái)獲得更多的事例和想法,或深刻的見(jiàn)解,請(qǐng)大家不吝賜教。
我先起個(gè)頭第一期是關(guān)于CFile及其派生類(lèi)
Use CFile and its derived classes for general-purpose disk I/O. Use ofstream or other Microsoft iostream classes for formatted text sent to a disk file.
It directly provides unbuffered, binary disk input/output services, and it indirectly supports text files and memory files through its derived classes.
Note that deleting the CFile object does not delete the physical file in the file system.
If you have not closed the file before destroying the object, the destructor closes it for you.
If you used new to allocate the CFile object on the heap, then you must delete it after closing the file. Close sets m_hFile to CFile::hFileNull.
So the member function Duplicate() should be use with Close() function, because of a new object which is created.
以上是個(gè)人在作例子和瀏覽msdn獲得的一些結(jié)論,當(dāng)然,除了單純的針對(duì)本問(wèn)題的關(guān)鍵是如何解決文件的隨機(jī)讀寫(xiě),同時(shí)就是詳細(xì)的文件輸入和輸出操作的結(jié)論,希望各位能夠把自己遇到的關(guān)于文件輸入輸出操作的問(wèn)題或解決方案共享出來(lái),使我們每個(gè)人都能從中受益,同時(shí)享受技術(shù)帶給我們的那份喜悅
Feedback
我想這個(gè), VS應(yīng)該提供了相應(yīng)的API來(lái)操作其文件,這應(yīng)該是最便捷,準(zhǔn)確的方法吧。
@SmartPtr
不過(guò)那就沒(méi)有可移植性了。
其實(shí),這個(gè)功能,VisualStudio的C++編譯器已經(jīng)提供了。
/FI[ ]pathname
此選項(xiàng)使預(yù)處理器處理指定的頭文件。此選項(xiàng)的作用與在命令行上、CL 環(huán)境變量中或命令文件中指定源文件,并同時(shí)在每個(gè)源文件的第一行上的 #include 指令中用雙引號(hào)指定該頭文件相同。如果使用多個(gè) /FI 選項(xiàng),將按 CL 處理文件的順序來(lái)包含這些文件。
在 Visual Studio 開(kāi)發(fā)環(huán)境中設(shè)置此編譯器選項(xiàng)
打開(kāi)此項(xiàng)目的“屬性頁(yè)”對(duì)話框。有關(guān)詳細(xì)信息,請(qǐng)參見(jiàn)設(shè)置 Visual C++ 項(xiàng)目屬性。
單擊“C/C++”文件夾。
單擊“高級(jí)”屬性頁(yè)。
修改“強(qiáng)制包含”屬性。
在source insight里直接寫(xiě)個(gè)腳本不就行了