把文本文件按行读到一个stringList中,使用什么Class。
请写出代码,谢谢!

解决方案 »

  1.   

    // example for CStdioFile::CStdioFile
    char* pFileName = "test.dat";
    CStdioFile f1;
    if( !f1.Open( pFileName, CFile::modeCreate
           | CFile::modeWrite | CFile::typeText ) ) {
       #ifdef _DEBUG
          afxDump << "Unable to open file" << "\n";
       #endif
       exit( 1 );
    }
    CStdioFile f2( stdout );
    TRY
    {
       CStdioFile f3( pFileName,
          CFile::modeCreate | CFile::modeWrite | CFile::typeText );
    }
    CATCH( CFileException, e )
    {
       #ifdef _DEBUG
          afxDump << "File could not be opened "
                  << e->m_cause << "\n";
       #endif
    }
    END_CATCH// example for CStdioFile::ReadString
    extern CStdioFile f;
    char buf[100];f.ReadString( buf, 99 );
      

  2.   

    he_zhidan(何志丹:www.vcshare.net) :
    你好,我刚学VC++,最近才上CSDN问VC的问题,这几天你回答了我不少问题,所以引起我对你的注意,刚才去访问了www.vcshare.net,原来你是湖北人。我在湖北生活了19年,我们也算半个老乡。
    我是初学者,希望今后多多关照,谢谢!
      

  3.   

    自己写,不要使用MFC的类,MFC很多意外情况。
    ================================================== FILE *pSourFile; //指向源文件
    CString sSourLine=""; //从源文件中取出的一行 pSourFile = fopen(sFileName,"rt");
    if(pSourFile==NULL) {
    AfxMessageBox("源文件打开失败!");
    return false;
    }
    while(!feof(pSourFile)){
    ReadLine(pSourFile, sSourLine);//读一行
    }
    }
    //读取文件的一行
    void CSetEx::ReadLine(FILE *pFile, CString &line)
    {
    line="";
    char ch;
    int pos=0;
    while(!feof(pFile)){
    //Read(&ch,1);
    ch = fgetc(pFile);
    if(ch=='\r'||ch=='\n')break;
    line+=ch;
    pos++;
    if(line.Find("", 0))
    {
    ftell(pFile);
    }
    }
    line.TrimRight('\r'); 
    line.TrimRight('\n'); 
    line.TrimRight('\r'); 
    line.TrimRight(0xff); //过滤文件结束符
    return;

    }
    //读取Unicode的字符
    void inline ReadLineW(FILE *pFile, BSTR &bstr)
    {
    bstr = NULL;
    wchar_t wch;
    int pos=0;
    BSTR bstrTmp;
    while(!feof(pFile)){
    wch = fgetwc(pFile);
    if(wch==-1) continue;
    if(wch==_T('\r')||wch==_T('\n'))break;
    bstr+=wch;
    bstrTmp+=wch;
    pos++;
    } return;
    }
      

  4.   

    我要实现的功能是读下面文件。文本文件内容如下:/*------------------
          注解
    ---------------*///注解CTRLDATATYPE     g_awViewClamp[] = {
                DLG_PANEL,     10, 0*16   ,276, 11*16+4, CP_PANEL1,            //DLG_STATIC,   23*8, 0*16  ,11, CS_STATIC1, VW_OVERVIEW_CLAMPPOSITION,
                DLG_STATIC,   14*8, 0*16+1, 4, CS_STATIC2, VW_CLAMP_PRESS      ,    //注解
                DLG_STATIC,   20*8, 0*16+1, 4, CS_STATIC2, VW_CLAMP_SPEED      ,
                DLG_STATIC,   26*8, 0*16+1, 8, CS_STATIC2, VW_CLAMP_ENDPOSITION,    //注解
                DLG_END
                };程序要求:
    1.去除注解
    2.找到 “CTRLDATATYPE”,然后读 { }直接的内容
      

  5.   

    CSDN沒有修改帖子的功能,上貼有問題,我重帖。
    我要实现的功能是读下面文件。文本文件内容如下:/*------------------
          注解
    ---------------*///注解CTRLDATATYPE     g_awViewClamp[] = {
       DLG_PANEL,     10, 0*16   ,276, 11*16+4, CP_PANEL1,   //DLG_STATIC,   23*8, 0*16  ,11, CS_STATIC1, VW_OVERVIEW_CLAMPPOSITION,
       DLG_STATIC,   14*8, 0*16+1, 4, CS_STATIC2, VW_CLAMP_PRESS      ,    //注解
       DLG_STATIC,   20*8, 0*16+1, 4, CS_STATIC2, VW_CLAMP_SPEED      ,
       DLG_STATIC,   26*8, 0*16+1, 8, CS_STATIC2, VW_CLAMP_ENDPOSITION,    //注解
       DLG_END
       };程序要求:
    1.去除注解,去除空行,空格。
    2.找到 “CTRLDATATYPE”,然后读 { }之間的内容。
      

  6.   

    我另開了一帖。
    http://community.csdn.net/Expert/topic/3478/3478693.xml?temp=.5955011
      

  7.   

    void CCsdn21dlgDlg::OnButton11() 
    {
    const CString strChangeLine = "\r\n";//换行符,有的地方\n就可以换行
    CFile file;
    file.Open("a1.txt",CFile::modeRead|CFile::shareDenyNone); CString strCotent;
    const int nLeng = file.GetLength() ;
    file.Read(strCotent.GetBuffer(nLeng + 1),nLeng);
    strCotent.ReleaseBuffer(); //除空格
    strCotent.Remove(' ');

    //除注释(假设没有递归注释)    
    //除去/* */
    while(true)
    {
    int nPosBegin = strCotent.Find("/*");
    int nPosEnd   = strCotent.Find("*/");
    if(-1 == nPosBegin || -1 == nPosEnd)
    break;
    strCotent.Delete(nPosBegin,nPosEnd - nPosBegin + 2);
    } //除去行注释
    while(true)
    {
    int nPosBegin = strCotent.Find("//");
    int nPosEnd   = strCotent.Find(strChangeLine,nPosBegin);
    if(-1 == nPosBegin || -1 == nPosEnd)
    break;
    strCotent.Delete(nPosBegin,nPosEnd - nPosBegin + 2);
    }

        //如果有两个连续的换行符删除一个
    int nPosEnd = 0 ;
    while(true)
    {
    int nPosBegin = strCotent.Find(strChangeLine,nPosEnd);
    nPosEnd   = strCotent.Find(strChangeLine,nPosBegin+1);
    if(-1 == nPosBegin || -1 == nPosEnd)
    break;
    if(nPosEnd - nPosBegin == strChangeLine.GetLength())
    {
    strCotent.Delete(nPosBegin,strChangeLine.GetLength());
    nPosEnd -= strChangeLine.GetLength();
    }
    }
    //如果以换行符开始删除
    if(0 == strCotent.Find(strChangeLine))
    strCotent = strCotent.Mid(strChangeLine.GetLength());

    //把中间结果放进文件便于调试
    CFile file2;
    file2.Open("a2.txt",CFile::modeCreate|CFile::modeWrite);
    file2.Write(strCotent,strCotent.GetLength());
    file2.Close(); //取得{}的内容
    int nPosBegin = strCotent.Find("{");
    nPosEnd = strCotent.Find("}");
    strCotent = strCotent.Mid(nPosBegin + 1 ,  nPosEnd - nPosBegin - 1); //用标号分隔的部分取出
    CStringArray arStr;
    while(true)
    {
    int nPos = strCotent.Find(",");
    CString str ;
    if(-1 == nPos)
    {
    str = strCotent;
    arStr.Add(str);
    break;
    }
    else 
    {
    str = strCotent.Left(nPos );
    strCotent = strCotent.Mid(nPos + 1);
    arStr.Add(str);
    }
    }
    file.Close();
    }