Dilog控键函数的实现代码(功能描诉:通过COMBOX控键向文本中写内容
写的格式和方法如下:
[1]
字符串............
字符串............
字符串............
结束标志
[2]
字符串............
字符串............
字符串............
结束标志
.............
写的过程中还要实现排序的功能!
当添加时通过找标号确定位置写在结束标志的那行下面的内容下移写入)
代码如下我现在不能实现不知道那里出问题了:
{
UpdateData(true);
if(m_combox=="")
{
AfxMessageBox("内容为空");
}
CString str;
CString Num;
CFileFind finder;
CStdioFile hTxtFile;
if(!finder.FindFile("c:\\*****.ini"))///判断文件是否存在
hTxtFile.Open("c:\\*****.ini",CFile::modeReadWrite|CFile::modeCreate);
else
hTxtFile.Open("c:\\*****.ini",CFile::modeReadWrite);
if(hTxtFile.ReadString(str))///读去文件内容如果内容为空就直接写入
{
char sRead[128];
hTxtFile.ReadString(sRead,(sizeof(sRead)-1));
str=sRead;
if(sRead[0]=='[')
{
Num.Format("%d",m_num);
if(sRead[1]==m_num&&str=='*')///"*"为结束标志
{
hTxtFile.WriteString(m_combox);
hTxtFile.WriteString("\n");
hTxtFile.WriteString("*");
hTxtFile.WriteString("\n");
}
else
{
if(sRead[1]>m_num)
{
shouji=1;///为实现排序用的标志很笨的方法呵呵!!!!!1
}
else
{
shouji=2;
}
if(shouji==2)
{
hTxtFile.WriteString("[");
hTxtFile.WriteString(Num);
hTxtFile.WriteString("]");
hTxtFile.WriteString("\r\n");
hTxtFile.WriteString(m_combox);
hTxtFile.WriteString("\n");
hTxtFile.WriteString("*");
hTxtFile.WriteString("\n");
hTxtFile.WriteString(str);
hTxtFile.WriteString("\n");
}
}
}
else///当原来的文件为空时直接写入或者添加的编号大于已经写入时在尾部添加
{
hTxtFile.SeekToEnd();
hTxtFile.WriteString("[");
hTxtFile.WriteString(Num);
hTxtFile.WriteString("]");
hTxtFile.WriteString("\r\n");
hTxtFile.WriteString(m_combox);
hTxtFile.WriteString("\n");
hTxtFile.WriteString("*");
hTxtFile.WriteString("\n");
}
}
hTxtFile.Close();
}
我现在编译没有错误就是没有结果写入不知道什么地方出问题了还有劳大侠门帮我看看
先谢谢各位了!!!
写的格式和方法如下:
[1]
字符串............
字符串............
字符串............
结束标志
[2]
字符串............
字符串............
字符串............
结束标志
.............
写的过程中还要实现排序的功能!
当添加时通过找标号确定位置写在结束标志的那行下面的内容下移写入)
代码如下我现在不能实现不知道那里出问题了:
{
UpdateData(true);
if(m_combox=="")
{
AfxMessageBox("内容为空");
}
CString str;
CString Num;
CFileFind finder;
CStdioFile hTxtFile;
if(!finder.FindFile("c:\\*****.ini"))///判断文件是否存在
hTxtFile.Open("c:\\*****.ini",CFile::modeReadWrite|CFile::modeCreate);
else
hTxtFile.Open("c:\\*****.ini",CFile::modeReadWrite);
if(hTxtFile.ReadString(str))///读去文件内容如果内容为空就直接写入
{
char sRead[128];
hTxtFile.ReadString(sRead,(sizeof(sRead)-1));
str=sRead;
if(sRead[0]=='[')
{
Num.Format("%d",m_num);
if(sRead[1]==m_num&&str=='*')///"*"为结束标志
{
hTxtFile.WriteString(m_combox);
hTxtFile.WriteString("\n");
hTxtFile.WriteString("*");
hTxtFile.WriteString("\n");
}
else
{
if(sRead[1]>m_num)
{
shouji=1;///为实现排序用的标志很笨的方法呵呵!!!!!1
}
else
{
shouji=2;
}
if(shouji==2)
{
hTxtFile.WriteString("[");
hTxtFile.WriteString(Num);
hTxtFile.WriteString("]");
hTxtFile.WriteString("\r\n");
hTxtFile.WriteString(m_combox);
hTxtFile.WriteString("\n");
hTxtFile.WriteString("*");
hTxtFile.WriteString("\n");
hTxtFile.WriteString(str);
hTxtFile.WriteString("\n");
}
}
}
else///当原来的文件为空时直接写入或者添加的编号大于已经写入时在尾部添加
{
hTxtFile.SeekToEnd();
hTxtFile.WriteString("[");
hTxtFile.WriteString(Num);
hTxtFile.WriteString("]");
hTxtFile.WriteString("\r\n");
hTxtFile.WriteString(m_combox);
hTxtFile.WriteString("\n");
hTxtFile.WriteString("*");
hTxtFile.WriteString("\n");
}
}
hTxtFile.Close();
}
我现在编译没有错误就是没有结果写入不知道什么地方出问题了还有劳大侠门帮我看看
先谢谢各位了!!!
CString strSection = "CComBox_1";
for(int i=0; i<m_comboxCtrl.GetCount(); i++)
{
CString strItem = _T("");
strItem.Format("Item_%d",i);
CString strVal = _T("");
m_comboxCtrl.GetLBText(i,strVal);
pApp->WriteProfileString(strSection, strItem, strVal);
}
//调取 m_comboxCtrl.ResetContent();
CWinApp* pApp = AfxGetApp();
CString strSection = "CComBox_1";
while(1)
{
CString strItem = _T("");
strItem.Format("Item_%d",i);
CString strVal =
strVal = pApp->GetProfileString(strSection, strItem);
if(strVal.IsEmpty())
break;
m_comboxCtrl.AddString(strVal);
}
怎么使用ini文件的绝对路径?
还有不用绝对路径也行吧!!!
TO:aa3000
问题中的c:\\*****.ini我用*代表文件名的省略
TO:my_girlf
我加了下面几句没有消息弹出:
BOOL p=hTxtFile.Open("c:\\*****.ini",CFile::modeReadWrite);
if(!p)
{
AfxMessageBox("文件打开失败");
}
在实验
zgl7903的方法!!!
十分感谢
现在在考虑排序问题!!!
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]
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".
到时候读取的时候也方便!!!!