简体   繁体   English

c ++ mfc csv文件读取

[英]c++ mfc csv file read

i have a problem with csv file reading. 我在读取csv文件时遇到问题。 I'm pretty new to mfc and i hope someone can help me. 我对mfc还是很陌生,希望有人能帮助我。 So...i have a button and with it i open file dialog and choose csv file. 所以...我有一个按钮,用它打开文件对话框,然后选择csv文件。 In csv file i have diferent shapes(rectangle,ellipse,pollygon) with color and position info(separtor is ;). 在csv文件中,我具有颜色和位置信息(分隔符为;)的不同形状(矩形,椭圆形,多边形)。 Now i need to show this informations in a ListBox and here i'm stuck. 现在,我需要在ListBox中显示此信息,在这里我被卡住了。 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之后的分号:

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. 意味着AddString()ReadString()失败后将仅被调用一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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