文本文件中有两行
第一行是combobox中的项目,以“,”分割项目,如:111,222,333
判断出换行后
第二行是listbox中的项目,以“,”分割项目,如:111,aaa,ccc求教高手这样设计这段程序?

解决方案 »

  1.   

    CStdioFile file; if( !file.Open("c:\\1.txt", CFile::modeRead) ) { AfxMessageBox("can not open file!"); return; }
    CString strLine;
    int nLine=0;
    CStringArray arr[2]; while(file.ReadString(strLine)) {
    nLine++; for(int i=0; i<arrChapter.GetSize(); i++) { char *ss=strLine.GetBufferSetLength(strLine.GetLength());  char *p; for ( p=strtok(ss, ","); p!=NULL; p=strtok(NULL, ",") ) { arr[nLine].Add(p); } }
    file.Close();
    for(int i=0; i<arr[0].GetSize(); i++)
    {
    m_combo.AddString(arr[0][i]);
    m_listbox.AddString(arr[1][i]);
    }
      

  2.   

    三,这个是读文件的噢,程序中好多编译错误~
    for(int i=0; i<arrChapter.GetSize(); i++)这句话中的arrChapter没定义?
      

  3.   

    for(int i=0; i<arrChapter.GetSize(); i++)这句代码多余的,,删除
      

  4.   

    程序运行的时候出错,不能运行,编译通过!
    还有,问个问题,为什么下面这段代码要放在file.close();后面呢?
    for(int i=0; i<arr[0].GetSize(); i++)
    {
    m_combo.AddString(arr[0][i]);
    m_listbox.AddString(arr[1][i]);
    }
      

  5.   

    CStdioFile file; if( !file.Open("c:\\1.txt", CFile::modeRead) )
    {
    AfxMessageBox("can not open file!");
    return;
    } CString strLine;
    int nLine=0;
    CStringArray arr[2]; while(file.ReadString(strLine))
    {
    char *ss=strLine.GetBufferSetLength(strLine.GetLength()); 
    char *p;
    for ( p=strtok(ss, ","); p!=NULL; p=strtok(NULL, ",") )
    {
    arr[nLine].Add(p);
    }
    nLine++;
    }
    file.Close(); for(int i=0; i<arr[0].GetSize(); i++)
    {
    m_combo1.AddString(arr[0][i]);
    m_listbox.AddString(arr[1][i]);
    }我运行过了,正确的
      

  6.   

    你那1.txt文件里的格式是怎样的?我出错运行不起来估计就是和你那文件里的格式不对~
    我那格式是这样的:111,222,AAA
                      222,333,444
      

  7.   

    原来的代码有误,
    刚才贴的才对。原来代码中,nLine++;的位置不对。。
      

  8.   

    CStdioFile file; if( !file.Open("c:\\1.txt", CFile::modeRead) )
    {
    AfxMessageBox("can not open file!");
    return;
    } CString strLine;
    int nLine=0;
    CStringArray arr[2]; while(file.ReadString(strLine))
    {
    strLine.TrimLeft();
    strLine.TrimRight();
    if( strLine.IsEmpty() )
    continue;
    char *ss=strLine.GetBufferSetLength(strLine.GetLength()); 
    char *p;
    for ( p=strtok(ss, ","); p!=NULL; p=strtok(NULL, ",") )
    {
    arr[nLine].Add(p);
    }
    nLine++;
    }
    file.Close(); for(int i=0; i<arr[0].GetSize(); i++)
    {
    m_combo1.AddString(arr[0][i]);
    m_listbox.AddString(arr[1][i]);
    }