如何从txt读出数据到数组呢?txt中的数据为 
1        34 
2        58 
3        348 
... 
... 
100      32 左边一列是序号,右边一列是数据 我想把右边的数据保存到一个数组中,然后做其他的用处。 想用CFileDialog对话框选择txt文件,然后保存到数组里。怎么做呢, 谢谢大家啊 

解决方案 »

  1.   

    用CStdioFile中的ReadString来逐行读取文件,用CString中的Tokenize来进行数据的获取,用atoi来转化字符串为整数
      

  2.   


    char szBuf[MAX_PATH];
    CStdioFile csf;
    CString str;
    CDWordArray cdwa;
    CFileDialog dlgOpen(TRUE,
    NULL,
    NULL,
    OFN_PATHMUSTEXIST,
    "Text File(*.txt)|*.txt|All File(*.*)|*.*|",
    this);
    dlgOpen.m_ofn.nMaxFile = 1;
    dlgOpen.m_ofn.lpstrInitialDir = "C:\\";
    dlgOpen.m_ofn.lpstrFile = szBuf;
    dlgOpen.m_ofn.lpstrFile[0] = NULL;
    if (IDOK == dlgOpen.DoModal())
    {
    if (csf.Open(dlgOpen.m_ofn.lpstrFile, CFile::modeRead | CFile::shareDenyNone | CFile::typeText))
    {
    while (csf.ReadString(str))
    cdwa.Add(atoi(str.Right(str.GetLength() - str.Find('\t') - 1)));
    csf.Close();
    }
    }
      

  3.   

    给一段代码:
    #define MAX_ARRAY_LEN 256 CFileDialog dlg(TRUE);
    int buf[MAX_ARRAY_LEN] = {0};
    if(IDOK == dlg.DoModal())
    {
    CString strPath = dlg.GetPathName();
    try
    {
    CStdioFile file;
    file.Open(strPath, CFile::modeRead);
    CString strText(_T(""));
    int nIndex = 0;
    while(file.ReadString(strText))
    {
    CString resToken(_T(""));
    int curPos = 0;
    int nCount = 0;
    CString strInfo(_T(""));
    CString strToken = _T(" ");
    resToken = strText.Tokenize(strToken, curPos);
    while(resToken != _T(""))
    {
    if((nCount++) % 2)
    {
    buf[nIndex++] = _ttoi(resToken);
    }
    resToken = strText.Tokenize(strToken, curPos);
    }
    }
    file.Close(); CString strMsg(_T(""));
    CString str(_T(""));
    for(int i=0; i<nIndex; i++)
    {
    str.Format(_T("%d - %d\r\n"), i, buf[i]);
    strMsg += str;
    }
    AfxMessageBox(strMsg);
    }
    catch (CFileException* e)
    {
    e->ReportError();
    e->Delete();
    }
    }
      

  4.   


    no money, no good