void CBtnLoadDlg::OnBnClickedBtnLoad()
{
printf("[OnBnClickedBtnOpen] \n");
CStdioFile ftxt;
CFileException kEx;
CFileDialog dlg(TRUE, _T("*.csv"), NULL, OFN_FILEMUSTEXIST, _T("CSV Files(*.csv)|*.csv|"), NULL);
if (dlg.DoModal() == IDOK)
{
// Get file name
m_strPath = dlg.GetPathName();
char charPtr[500];
sprintf(charPtr, "%S", m_strPath);
CString file_name = dlg.GetFileName();
CString m_strStatus_temp;
m_strStatus += "File name: ";
m_strStatus += file_name;
m_strStatus += "\r\n";
// Set path to load
m_strStatus += "File path: ";
m_strStatus += m_strPath;
m_strStatus += "\r\n";
ifp = fopen(charPtr, "r");
// Save folder path for further saving
m_strFolderPath = m_strPath.Left(m_strPath.GetLength() - file_name.GetLength());
m_strStatus += "Folder path: ";
m_strStatus += m_strFolderPath;
m_strStatus += "\r\n";
// Load file using ifp
// 1. Count number of items
int nr_data = 0;
for (int i = 0;; i++){
int doc_idx, word_idx, nr_word;
if (fscanf(ifp, "%d,%d,%d", &doc_idx, &word_idx, &nr_word) == EOF)
break;
// count number of items
nr_data++;
}
CString strTemp; strTemp.Format(L"nr_data: %d \r\n", nr_data);
m_strStatus += strTemp;
// 2. Allocate memory & Load
rewind(ifp);
data = (int**)malloc(nr_data*sizeof(int*));
for (int i = 0; i < nr_data; i++) {
data[i] = (int*)malloc(3 * sizeof(int));
if (fscanf(ifp, "%d,%d,%d", &data[i][0], &data[i][1], &data[i][2]) == EOF)
break;
else
{
printf("%d: %d / %d / %d \n", i, data[i][0], data[i][1], data[i][2]);
}
}
// 3. Show data loaded
for (int i = 0; i < nr_data; i++)
{
CString strTemp; strTemp.Format(L"[%d/%d] / %d %d %d \r\n"
, i + 1, nr_data, data[i][0], data[i][1], data[i][2]);
m_strStatus += strTemp;
}
// 가장 최근 줄에 focus가 가도록 한다. (왜 잘 동작안할까..)
int str_len = m_ctrlStatus.GetLineCount();
m_ctrlStatus.LineScroll(str_len);
// Update text
UpdateData(FALSE);
}
}