各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的。本文将对Visual C++中有关文件操作进行全面的介绍,并对在文件操作中经常遇到的一些疑难问题进行详细的分析。
4.临时文件的使用 在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下:
一.将信息写入.INI文件中. 1.所用的WINAPI函数原型为: BOOL WritePrivateProfileString( 其中各参数的意义: LPCTSTR lpAppName 是INI文件中的一个字段名. LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名. LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的. LPCTSTR lpFileName 是完整的INI文件名. 2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:\stud\student.ini 文件中. CString strName,strTemp; 此时c:\stud\student.ini文件中的内容如下: [StudentInfo] 3.要将学生的年龄保存下来,只需将整型的值变为字符型即可: strTemp.Format("%d",nAge);
1.所用的WINAPI函数原型为: DWORD GetPrivateProfileString( 其中各参数的意义: 前二个参数与 WritePrivateProfileString中的意义一样. lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量. lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器. nSize : 目的缓存器的大小. lpFileName : 是完整的INI文件名. 2.具体使用方法:现要将上一步中写入的学生的信息读入程序中. CString strStudName; 执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名". 3.读入整型值要用另一个WINAPI函数: UINT GetPrivateProfileInt( 这里的参数意义与上相同.使用方法如下: nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");
1.写入: CString strTemp,strTempA; 2.读出: nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini"); file://使用strTempA中的内容. } 补充四点: 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. 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. 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, 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. CFile的派生类CStdioFile提供了对文件进行流式的操作功能。其中函数void CStdioFile::WriteString( LPCTSTR lpsz )写入一个字符串,需要给字符串lpsz的末尾加上换行标志”\r\n”;函数bool CStdioFile::ReadString(CString &rString )从文件中读取一行,如果文件未读完返回true,否则返回false。
比如:写入文件的例子 //创建文件
//写入文件 CString str; //关闭文件 file.Close(); 比如:读文件的例子 CString strText = “”; //打开文件 //逐行读取字符串 MessageBox(strText); //关闭文件 file.Close(); Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1573774 |
|
来自: yiherainbow > 《我的图书馆》