以文本文件data.txt为例,其内容有:
9
423656.156250 140365.812500
423686.406250 140380.328125
423807.656250 140391.515625
423829.156250 140380.359375
423863.656250 140355.437500
423899.343750 140365.109375
423925.218750 140348.250000
423962.312500 140343.953125
423961.343750 140378.515625
5
435542.375000 140210.515625
435504.906250 140221.265625
435493.531250 140244.921875
435531.625000 140246.546875
435550.562500 140218.937500    此文本的数据为坐标值,左边的为x,右边的为y。这里有2组数据,9代表该组有9个数据,5代表该组有5个数据(编写时还请考虑到数据很多的情况,应该会上千个,谢谢)。现在要读取这个文件的数据,并画图用线将每组的数据所代表的点连接起来。应该怎么做,请高手帮忙。本人是菜鸟,还请高手写出全部程序,越详细越好,亿分感谢!!!

解决方案 »

  1.   

    补充说明:最好用Vc++,CFile类,谢谢阿
      

  2.   

    用 CStdioFile来处理吧,分析每一行!
      

  3.   

    以前遇到个这种问题,主要用CFile类,
      cfile.open(.dat文件)
      for(i=0,i=行数,i++)
      Cfile.read(...)
       Cfile.close()  
        
      

  4.   

    不好意思, 写错了CFile.open(exe文件)
      要注意每行中的分隔符
      

  5.   

    void CCFileView::OnDraw(CDC* pDC)
    {
    CCFileDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    CPoint point;//保存点坐标
    DWORD length;
    DWORD readlength=0;
    int num;//有几行数
    int i=1;//循环计数
    char pBuf[24];//缓冲区
    memset(pBuf,0,24);//缓冲区设置为0
    byte count=-1;//缓冲区计数
    CFile file;//定义文件类
    file.Open("data.txt",CFile::modeCreate | CFile::modeNoTruncate);//打开文件
    length=file.GetLength();

    while(readlength<length)
    {
    //读入某组坐标的个数
    do
    {
    count++;
    file.Read(pBuf+count,1);
    readlength++;

    } while(pBuf[count]!='\n');
    pBuf[count]=0;

    CString str;
    str.Format("%s",pBuf);

    num=atoi(str);

    //获得第一个点的坐标
    pBuf[13]=0;//x和y为12,第13个设置为0
    file.Read(pBuf,13);
    readlength+=13;
    point.x=atoi(pBuf);
    file.Seek(2,CFile::current);
    readlength+=2;
    file.Read(pBuf,13);
    readlength+=13;
    point.y=atoi(pBuf);
    pDC->MoveTo(point);

    file.Seek(2,CFile::current);
    readlength+=2;
    for(;i<num;i++)
    {
    file.Read(pBuf,13);
    readlength+=13;
    point.x=atoi(pBuf);
    file.Seek(2,CFile::current);
    readlength+=2;
    file.Read(pBuf,13);
    readlength+=13;
    point.y=atoi(pBuf);
    pDC->LineTo(point);
    file.Seek(2,CFile::current);
    readlength+=2;
    }
    if(readlength!=length)
    file.Seek(2,CFile::current);
    }
    //关闭文件
    file.Close();
    }写的很乱:(,希望能帮上忙
      

  6.   

    CClientDC dc(this);
    CStdioFile sf("data.txt",CFile::modeRead);
    CString strLine;
    while(sf.ReadString(strLine))
    {
    int nPoints=atoi(strLine);
    POINT *pPoints=new POINT[nPoints];
    for(int i=0;i<nPoints;++i)
    {
    sf.ReadString(strLine);
    pPoints[i].x=atof(strLine)/100;
    pPoints[i].y=atof(strLine.Mid(strLine.Find('\t')))/100;
    }
    dc.Polygon(pPoints,nPoints);
    delete(pPoints);
    }
      

  7.   

    建议大家不要写出全部代码,一是影响CSDN的风气,二是影响中国程序员的风气。
      

  8.   

    h39394(trueman) 说得好 !!!
    支持!
    主要描述一下思路和重要点就可以了 
    实在是需要程序帮助可以另外联系
      

  9.   

    用CStdioFile方便些,CFile处理有特殊字符或者二进制的时候要小心0x0A, 0x0D这样的数据。做法见楼上各位.
      

  10.   

    CStdioFile 的ReadString一行行的读入,读入后,先得到有几组数据,然后用循环读出所有坐标,这些都比较基础,楼主自己动手试试