weidagang2046的專欄

          物格而后知致
          隨筆 - 8, 文章 - 409, 評(píng)論 - 101, 引用 - 0

          導(dǎo)航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(12)

          隨筆檔案(8)

          文章分類(421)

          文章檔案(409)

          相冊(cè)

          Link

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          CFile Example

          CFile is the class used for handling Files in MFC. This class can be used for creating, reading, writing and modifying files. It directly provides unbuffered, binary disk input/output services, and it indirectly supports text files and memory files through its derived classes.

          CFile - Creating a File:

          ?? There are two ways of creating files. One way is to instantiate the CFile object with the file path. This creates the file. The second way is to call the Open function. This also creates the file.

          CFile cfile_object( "c:\\test\\codersource_cfile_example.txt", CFile::modeCreate|CFile:: modeReadWrite);

          ???? CFile cfile_object;
          ???? cfile_object.Open( "c:\\test\\codersource_cfile_example.txt",? CFile::modeCreate|CFile:: modeReadWrite);
          ???
          ??
          The first parameter to both the functions (CFile() constructor and Open()) is the physical path of the file in the disk.?The second parameter is an enumerated constant. This specifies the mode of opening the file object. The above constants?modeCreate implies "create a new file" and modeReadWrite means "open the file for both reading and writing".

          ?? If the file is opened without specifying the mode constant shareDenyNone, this file can be opened in read mode by?other programs. This feature will be necessary for text files, logs created by programs. For creating text files we use?CFile::typeText and for binary files CFile::typeBinary.

          CFile - Writing to a File:

          ?? The function Write is used to write data to the files. The sample code is as follows.

          ???? CFile cfile_object;
          ???? cfile_object.Open( "c:\\test\\codersource_cfile_example.txt", CFile::modeCreate|CFile::modeWrite);

          ???? char szSampleText[100];
          ???? strcpy(szSampleText, "Sample Text for CFile Write function Example");
          ???? cfile_object.Write (szSampleText,100);?

          ?? If there is any need to write text line by line, it is better to use the class CStdioFile.

          CFile - Reading from a file:

          ?? The function Read is used to read data from files. The sample code is,

          ???? CFile cfile_object;
          ???? cfile_object.Open( "c:\\test\\codersource_cfile_example.txt", CFile::modeCreate|CFile::modeWrite);

          ???? char szSampleText[100];
          ???? UINT lBytesRead = cfile_object.Read (szSampleText,100);?

          ?? The function returns the number of bytes read from the file. The maximum number of characters read will be the second?parameter of the Read function.

          CFile - closing the file:

          ?? The Close function is used to close the file. But the close function need not be called, as the destructor will?automatically call it if the file is open. So when the object goes out of scope, the destructor calls close function.



          from: http://www.codersource.net/mfc_cfile.html

          posted on 2006-08-29 22:53 weidagang2046 閱讀(843) 評(píng)論(0)  編輯  收藏 所屬分類: Windows

          主站蜘蛛池模板: 华亭县| 临洮县| 彰化市| 武强县| 集安市| 澎湖县| 清流县| 溆浦县| 米泉市| 东山县| 吉首市| 永福县| 屯留县| 青海省| 景泰县| 北辰区| 哈尔滨市| 武川县| 怀安县| 兴文县| 随州市| 赣州市| 镇沅| 甘洛县| 莱阳市| 平遥县| 阿坝| 独山县| 乌海市| 淮滨县| 德惠市| 丰都县| 黑龙江省| 淳化县| 常州市| 黄冈市| 开阳县| 威远县| 合川市| 常德市| 崇阳县|