各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的。本文将对Visual C++中有关文件操作进行全面的介绍,并对在文件操作中经常遇到的一些疑难问题进行详细的分析。
在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下:
一.将信息写入.INI文件中.
1.所用的WINAPI函数原型为:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
其中各参数的意义:
LPCTSTR lpAppName 是INI文件中的一个字段名.
LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:/stud/student.ini 文件中.
CString strName,strTemp;
int nAge;
strName="张三";
nAge=12;
::WritePrivateProfileString("StudentInfo","Name",strName,"c://stud//student.ini");
此时c:/stud/student.ini文件中的内容如下:
[StudentInfo]
Name=张三
3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:
strTemp.Format("%d",nAge);
::WritePrivateProfileString("StudentInfo","Age",strTemp,"c://stud//student.ini");
二.将信息从INI文件中读入程序中的变量.
1.所用的WINAPI函数原型为:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
其中各参数的意义:
前二个参数与 WritePrivateProfileString中的意义一样.
lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.
2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.
CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c://stud//student.ini");
执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
3.读入整型值要用另一个WINAPI函数:
UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);
这里的参数意义与上相同.使用方法如下:
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c://stud//student.ini");
三.循环写入多个值,设现有一程序,要将最近使用的几个文件名保存下来,具体程序如下:
1.写入:
CString strTemp,strTempA;
int i;
int nCount=6;
file://共有6个文件名需要保存
for(i=0;i {strTemp.Format("%d",i);
strTempA=文件名;
file://文件名可以从数组,列表框等处取得.
::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
"c://usefile//usefile.ini");
}
strTemp.Format("%d",nCount);
::WritePrivateProfileString("FileCount","Count",strTemp,"c://usefile//usefile.ini");
file://将文件总数写入,以便读出.
2.读出:
nCount=::GetPrivateProfileInt("FileCount","Count",0,"c://usefile//usefile.ini");
for(i=0;i {strTemp.Format("%d",i);
strTemp="FileName"+strTemp;
::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c://usefile//usefile.ini");
file://使用strTempA中的内容.
}
补充四点:
1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.
2.文件名的路径中必须为 // ,因为在VC++中, // 才表示一个 / .
3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: ".//student.ini".
vc操作常用文件操作(三)CFileExample
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 constantsmodeCreate 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 byother programs. This feature will be necessary for text files, logs created by programs. For creating text files we useCFile::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 secondparameter 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 willautomatically 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
CFile的派生类CStdioFile提供了对文件进行流式的操作功能。其中函数void CStdioFile::WriteString( LPCTSTR lpsz )写入一个字符串,需要给字符串lpsz的末尾加上换行标志”/r/n”;函数bool CStdioFile::ReadString(CString &rString )从文件中读取一行,如果文件未读完返回true,否则返回false。
比如:写入文件的例子
//创建文件
CStdioFile file;
file.Open("ts.txt",CFile::modeCreate|CFile::modeWrite);
//写入文件
CString str;
str.Format("%s/r/n","hello!I am talkingmute!");
file.Seek(0,CFile::end);
file.WriteString( str );
//关闭文件
file.Close();
比如:读文件的例子
CString strText = “”;
CString szLine = “”;
//打开文件
CStdioFile file;
file.Open("ts.txt",CFile::modeRead);
//逐行读取字符串
while( file.ReadString( szLine ) )
{
strText += szLine;
}
MessageBox(strText);
//关闭文件
file.Close();
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1573774
相关推荐
vc操作ini文件
VC之PDF文件操作。博客地址:http://blog.csdn.net/bingdianlanxin,有问题欢迎留言!
使用VC6.0进行文件操作的示例,示范了如何通过vc读写文件.
vc 基于MFC的文件操作vc 基于MFC的文件操作vc 基于MFC的文件操作
基础级别的VC 文件操作实例,获得INI文件指定段的全部键名和键值、文件对话框、临时文件创建、目录创建、获得INI文件的全部段名、查找文件、复制文件、获得或设置进程的当前目录、获得或设置文件的属性、逐行读取...
VC中常用控件的使用方法
该程序主要用于vc6.0对文件的操作,读写。
VC--文件操作函数大家一起下载把 关于磁盘操作的
VC读写ini文件,VC读写ini文件.rar,VC读写ini文件.rar,VC读写ini文件.rar,VC读写ini文件.rar,
VC使用Shell操作文件,使用Shell复制文件/移动文件或删除文件等操作,简单但实用的一些小技巧,通过这些例子你会加深对Shell操作的理解.
VC读写操作文件.zip visual c++读写操作文件.zip
VC中常见的文件操作,.pdf格式。。。
VC实现从源文件到目的地址的复制操作 方法灵活简单
如何使用Shell操作文件 如何逐行读取文本文件 如何查找文件 如何创建临时文件 如何创建目录 如何复制文件 如何获得INI文件的全部段名 如何获得Windows目录和System目录 如何获得或设置文件的属性 如何获得...
VC中的文件读写操作[参考].pdf
VC文件读写操作实例
vc文件操作汇总—支持wince 其中包括: 1、判断文件及文件夹是否存在 2、创建及删除目录 3、查看指定根目录下目录个数 4、文件记录操作 5、CCeFileFind — wince
平时搜集的vc中文件的操作(比较全),包括对INI文件的操作实例等.
VC操作DBF数据库文件的类,用于读写dbf文件
用VC实现对excel文件的读操作和写操作。