15,979
社区成员
发帖
与我相关
我的任务
分享
// 【抽奖】按钮按下
void CLotteryDlg::OnBnClickedBtnLot()
{
// TODO: 在此添加控件通知处理程序代码
CString strFileName;
strFileName.Empty();
strFileName.Format("2013.csv");
ReadFile(strFileName); // 读取文件
return;
}
// 读取文件
BOOL CLotteryDlg::ReadFile(CString strFileName)
{
FILE *fRead;
fRead = fopen((const char *)strFileName, "rt");
if (fRead == NULL)
{
AfxMessageBox("无法读取数据文件");
return FALSE;
}
CStdioFile inFile(fRead);
CString strLine, strResult;
strLine.Empty();
strResult.Empty();
CStringArray astrTemp;
int nCount = 0;
while (inFile.ReadString(strLine)) // 循环读取每一行
{
astrTemp.Add(strLine);
strLine.Empty();
nCount++; // 计算行数
}
nCount--;// 去掉表头行
inFile.Close();
srand((unsigned)time(NULL));
int nLotNum = (rand() % nCount) + 1;
strLine = astrTemp[nLotNum]; // 得到抽奖结果的字符串
UpdateViewData(strLine); // 数据分割·画面更新
return TRUE;
}
// 数据分割·画面更新
void CLotteryDlg::UpdateViewData(CString strData)
{
CString strTemp;
strTemp.Empty();
int nCnt = 0, nSituation = 0;
while (nCnt < 3)
{
nSituation = 0;
strTemp.Empty();
if (nCnt < 2)
{
while ((unsigned char)strData[nSituation] != 44)
{
nSituation++;
}
strTemp = strData.Left(nSituation);
strData = strData.Right(strData.GetLength() - nSituation - 1);
nCnt++;
}
else
{
nCnt++;
strTemp = strData;
}
switch (nCnt)
{
case 1:
m_strName = strTemp; // 显示姓名
break;
case 2:
m_strTelPhone = strTemp; // 显示电话
break;
case 3:
m_strSex = strTemp; // 显示性别
break;
default:
break;
}
}
UpdateData(FALSE);
}