vc里如何操作txt文件呢,如果文件中的数据是这样的样子的话025  3491234  025  5319971   70           
025  4927038  010  62110045  236
025  7216340  025  4521009   310       
025  3491234  0571  2199516  100
025  8120321  021  81094532  50         怎么才能把这些数据一个个的读出来呢,能详细点吗?
谢谢各位了!

解决方案 »

  1.   

    用StdioFile的ReadString读出来
    在CString中把它们按照空格为间隔符号劈开来。
      

  2.   

    能打一些代码吗?谢谢,我这没有MSDN,以上面的数据举个例子好吗,假设上述数据在note.txt文件中,谢谢了
      

  3.   

    virtual LPTSTR ReadString( LPTSTR lpsz, UINT nMax );
    throw( CFileException );BOOL ReadString(CString& rString);
    throw( CFileException );Reads text data into a buffer, up to a limit of nMax–1 characters, from the file associated with the CStdioFile object. Reading is stopped by the first newline character. If, in that case, fewer than nMax–1 characters have been read, a newline character is stored in the buffer. A null character (‘\0’) is appended in either case. Example// example for CStdioFile::ReadString
    extern CStdioFile f;
    char buf[100];f.ReadString( buf, 99 );
    快升级了
    多给写分吧
      

  4.   

    f.ReadString( buf, 99 );
    里面的99是什么意思呢,空格它是怎么处理的呢,看上面的英文,是不是连空格一起读进buf里,遇到换行就停止呢,多行的话是不是可以用个循环呢?
    上面的一位朋友说在CString中把空格去掉的话,怎么去呢,那些一个个数据怎么分别读进不一样的变量里呢?
    谢谢了
      

  5.   

    int GetChar(CString &LineText, char *ch, int count)
    {
        int iPos = -1;
    CString text;
            int  i = 0; if(LineText.IsEmpty())
    return -1;        while(1)
    {
    iPos = LineText.Find(",");      
    if(iPos == -1)
    {
        ch[i] = atoi(LineText.GetBuffer(10)); //想办法保存Num
    i++;
        return i;  //这是一行结束
    }
    if(LineText.IsEmpty())
    continue;
    text = LineText.Left(iPos);
    ch[i] = atoi(text.GetBuffer(10));         //想办法保存Num
    i++;
    if(i >= count)
            return i;
    //LineText = LineText.Right(LineText.GetLength() - iPos - 1);
    LineText.Delete(0, iPos + 1);
    LineText.TrimLeft();
    } return -1;
    }
       CString     FileText;
        CString     LineText;
    CStdioFile  stFile;
    BOOL     ret = TRUE;
    int     c[160];
    CString FilePathName; stFile.Open("f:\\dd", CFile::modeRead | CFile::typeText);

    while(1)
    {
    ret = stFile.ReadString(LineText);
    if(!ret)
    {
        break;   //这是文件结束
    }

    LineText.TrimLeft();
    LineText.TrimRight();
    FileText = FileText + LineText + " ";
    }
         stFile.Close();
        
    while(1)
    {
    if(GetChar(FileText, c, 50) == -1)
    break;
    //你可以处理你的那160个字了
    }方法虽有点笨,但好控制
      

  6.   

    CString     FileText;
         CString     LineText;
    CStdioFile  stFile;
    BOOL     ret = TRUE;  
    CString     FilePathName;
    int     Data[5];    //你一行是5个数据,你可以自己改
    char        seps[]   = " \t\n";   //用于strtok()的分隔符,参考msdn
    char      *token;
    int i = 0; stFile.Open("f:\\dd", CFile::modeRead | CFile::typeText);

    while(1)
    {
    ret = stFile.ReadString(LineText);
    if(!ret)
    {
        break;               //这是文件结束
    }

    LineText.TrimLeft();   //去掉两端的多余的空格
    LineText.TrimRight();

    //如果你的文件格式有变化,你要修改
    i = 0;
    token = strtok( LineText.GetBuffer(LineText.GetLength()), seps );
    while(token != NULL)
    {
       Data[i] = atoi(token);
       token = strtok(NULL, seps);
       i++;
    }
    //你可以在下面处理得到的数字Data[5]

    }
        stFile.Close();
      

  7.   

    下面是我刚刚手写的一段改进代码
    #define ERRMSG CString strErr("读取短消息文件发生异常!异常原因为\n");TCHAR cErr[256];e->GetErrorMessage(cErr, sizeof(cErr));strErr += cErr;AfxMessageBox(strErr);THROW_LAST();int iTmp = 0;
    CString str;
    int iMsg[160][20];   //存放160个数据,根据需要可适当放大
    TCHAR cMsgTmp[20], cMsgLine[256];BOOL ReadMsgFile(TCHAR* pFile)   //存储短消息的文件名,以.TXT为扩展名
    {
    TRY
    {
    CFile file(pFile, CFile::modeRead);
    TRY
    {
    CArchive ar(&file, CArchive::load);
    while(1)
    {
    if(ar.ReadString(str)) //获取一行字符串
    {
    str.TrimLeft();
    str.TrimRight();
    }
    else    //到了文件末
    {
    ..........//处理收到的数据

    return FALSE;
    }
    lstrcpy(cMsgLine, (LPCTSTR)str);
    //解析字符串
    cMsgTmp = strtok(cMsgLine, ", ");
    while(cMsgTmp) //判断是否已经处理一行数据
    {
    iMsg[iTmp] = atoi(cMsgTmp); //转换为整数
    iTmp++;
    cMsgTmp = strtok(NULL, ", ");
    }
    }
    }
    CATCH(CMemoryException, e)
    {
    ERRMSG
    }
    AND_CATCH(CArchiveException, e)
    {
    ERRMSG
    }
    AND_CATCH(CFileException, e)
    {
    ERRMSG
    }
    END_CATCH
    }
    CATCH(CFileException, e)
    {
    ERRMSG
    }
    END_CATCH        return TRUE;
    }
      

  8.   

    补充:
    CString     FileText;
    CString     FilePathName;
    不要