现在有一个txt文档 里面有四行数字 比如
1
23
342
26
就是我怎么用最简短的语句完成把这四个数字依次赋值给int类型数组a[0]-a[3]呢
我之前用的方法都十分的笨拙 我想问下有没有最简单的方法

解决方案 »

  1.   

    for(int i=0;i<4;i++)
    {
    ReadString(str);
    a[i] = atoi(str);
    }
      

  2.   


    #include <iostream>int main(int argc, char& argv)
    {
    if (freopen("test.txt","r",stdin) == NULL)
    {
    std::cout << "file not found.\n";
    return -1;
    }
    int a[4]; std::cin >> a[0];
    std::cin >> a[1];
    std::cin >> a[2];
    std::cin >> a[3]; std::cout << a[0] << std::endl;
    std::cout << a[1] << std::endl;
    std::cout << a[2] << std::endl;
    std::cout << a[3] << std::endl;

    return 0;
    }
      

  3.   

    就是读文件 然后赋值 还想怎么办?
    CStdioFile m_file;
    CFileException m_excep;
    m_file.Open("data.txt", CFile::modeRead, &m_excep);
    CString str;
    int j = 0;
    while (m_file.ReadString(str))
    {
        a[j] = atoi(str);
        j++;
    }
    m_file.Close();
      

  4.   


    try
    {
    #define MAX_COUNT 10
    int a[MAX_COUNT] = {0};
    int nIndex = 0;
    CStdioFile file;
    file.Open(_T("F:\\11.txt"), CFile::modeRead);
    CString strText(_T(""));
    while(file.ReadString(strText))
    {
    strText.TrimLeft(_T(" "));
    strText.TrimRight(_T(" "));
    if(!strText.IsEmpty())
    {
    a[nIndex++%MAX_COUNT] = _ttoi(strText);
    }
    }
    file.Close();
    }
    catch (CException* e)
    {
    e->ReportError();
    e->Delete();
    }