567
社区成员




bool Intel200712::GetRandomData(int nTotal, const wchar_t *pchFileName) //获得试验数据
{
wstring stFileName;
if(NULL == pchFileName)
{
stFileName = GetCurrentDirW();
stFileName += _T("\\Students.in");
}
else
{
stFileName = pchFileName;
}
fstream fileRandom(stFileName.c_str(), ios::out);
if(fileRandom.fail())
{
int nError = ::GetLastError();
return false;
}
char chBuf[64];
memset(chBuf, 0, sizeof(chBuf));
sprintf(chBuf, "%d\n", nTotal);
fileRandom << chBuf;
for(int i = 0; i < nTotal; i ++)
{
memset(chBuf, 0, sizeof(chBuf));
sprintf(chBuf, "%010d ", i);
for(int j = 0; j < 50; j ++)
{
chBuf[11 + j] = rand() % 5 + '0';
}
chBuf[61] = '\n';
fileRandom << chBuf;
// fileRandom.write(chBuf, (std::streamsize)strlen(chBuf));
}
fileRandom.close();
return true;
}
bool Intel200712::ReadInputFile(const wchar_t *pchFileName)
{
wstring stFileName;
if(NULL == pchFileName)
{
stFileName = GetCurrentDirW();
stFileName += _T("\\Students.in");
}
else
{
stFileName = pchFileName;
}
// char chFileTime[MAX_PATH];
// memset(chFileTime, 0, sizeof(chFileTime));
// ::wcstombs(chFileTime, (wchar_t *)stFileName.c_str(), stFileName.size());
// cout<< chFileTime << endl;
// const wchar_t *pch = stFileName.c_str();
// wcout << pch << endl;
//为什么还必须放在这个地方,而放到main函数入口就会导致cout<<pchData无法显示
// setlocale(LC_ALL,"Chinese-simplified"); //这个函数是为了让VS2005能够识别中文文件名而特地大老远跑过来帮忙的
fstream fileRead;
fileRead.open(stFileName.c_str(), ios::in);
if(!fileRead.good())
{
unsigned long nErrorCode = ::GetLastError();
return false;
}
int nBufLen = 100;
char *pchBuf = new char[nBufLen]; //本例子100个长度足够了
memset(pchBuf, 0, nBufLen);
fileRead.getline(pchBuf, nBufLen - 1);
this->m_nStudentCount = atoi(pchBuf);
cout << pchBuf << endl;
this->m_pStudentQuestionary = new STUDENT_QUESTIONARY[this->m_nStudentCount];
memset(m_pStudentQuestionary, 0, sizeof(STUDENT_QUESTIONARY) * m_nStudentCount);
int nIndex = 0;
while(!fileRead.eof())
{
if(nIndex >= m_nStudentCount)
{
break;
}
fileRead.getline(pchBuf, nBufLen - 1);
// cout << pchBuf << endl;
m_pStudentQuestionary[nIndex ++] = ExplainQuestionaryInfo(pchBuf, (int)strlen(pchBuf));
//ExplainQuestionaryInfo用来解释读到的数据
}
fileRead.close();
delete[] pchBuf;
return true;
}