简体   繁体   中英

c++ mfc csv file read

i have a problem with csv file reading. I'm pretty new to mfc and i hope someone can help me. So...i have a button and with it i open file dialog and choose csv file. In csv file i have diferent shapes(rectangle,ellipse,pollygon) with color and position info(separtor is ;). Now i need to show this informations in a ListBox and here i'm stuck. I got so far(code)...and i don't know it's ok and i can't find any good help so i hope someone can give me a hint.

void CDialogDrawing::OnBnClickedButton2()
{

      TCHAR filtri[] = _T("CSV files (*.csv)|*.csv||"); 
      CString path; 

      CFileDialog dlg(TRUE, _T("csv"), _T("*.csv"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, filtri);
      dlg.m_ofn.lpstrTitle = _T("Open...");

      if(dlg.DoModal() == IDOK) //OK
      {
         path = dlg.GetPathName();
         //
         CStdioFile readFile;
         CFileException fileException;
         CString strLine;

         if(readFile.Open(path, CFile::modeRead, &fileException))
         {
             while (readFile.ReadString(strLine));
             {
                  seznamLikov.AddString(strLine);
             }
         }
         else
         {
             CString strErrorMsg;
             strErrorMsg.Format(_T("Can't open file %s , error : %u"), path, fileException.m_cause);
             AfxMessageBox(strErrorMsg);
    }
    readFile.Close();
 }
 }

Trailing semi-colon after the while :

while (readFile.ReadString(strLine));
{
    seznamLikov.AddString(strLine);
}

remove it as it is equivalent to:

while (readFile.ReadString(strLine)) {}

{
    seznamLikov.AddString(strLine);
}

meaning AddString() will be invoked only once, after ReadString() fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM