有TXT文件,格式如下:
1 2
3 4
5 6
...如何读取该文件并将每行的数据装换为CPoint类型的x、y坐标,然后再将这些CPoint存入一个数组?
如上述例子得到结果应为:
CPoint a[p1(1,2),p2(3,4),p3(5,6)...]急用,在线等...

解决方案 »

  1.   

    定义一个动态数组array
    使用CStdioFile读取,然后根据空格分解,解析,创建CPoint对象
    array.Add
      

  2.   

    呵呵,没有代码,自己查一下吧。CArray,CStdioFile什么的。
      

  3.   

     
    int i, nTemp, x, y;
    CStdioFile fileRead, fileSave;
    CString strRead, strTemp;
    fileRead.Open(filename, CFile::modeRead);
    while(fileRead.GetPosition()!=fileRead.GetLength())
    {
    fileRead.ReadString(strRead);
    nTemp = strRead.Find(" ");
    strTemp = strRead.Left(nTemp);
    x = atoi(strTemp);
    strTemp = strRead.Right(strRead.GetLength()-nTemp-1);
    y = atoi(strTemp);
    fileRead.Close();
    }
    大概就是这么个意思
    数组的问题你应该能搞定吧
      

  4.   

    假设文件内容放到了pszFileText字符串指针里,则可以先使用strtok按行拆分,然后再使用strchr按空格拆分,最后使用atoi转字符串转换为数字。
    char *pszNum;
    CPoint pt;
    pszRow = strtok("\r\n", pszFileText);
    while(NULL != pszRow)
    {
        pszNum = strchr(pszRow, ' ');
        pt.x = atoi(pszRow);
        pt.y = atoi(pszNum);
        pszRow = strtok(NULL, pszFileText);
    }
      

  5.   

    感谢xianglitian(向立天)!但是我用的是eVC,不支持ReadString函数,有没有别的办法啊? 
      

  6.   

    string filename;
    ifstream in(filename.c_str());
    int x,y;
    CPoint a[256];
    int i=0;
    while (!in.eof())
    {
    string line;
    getline(in,line);
    istringstream iss(line);
    iss>>x>>y;
    a[i] = CPoint(x,y);
    i++;
    }
      

  7.   

    scanf("%d%d",&x,&y);然后,传入 point.x,  point.y,然后加入到容器中。
      

  8.   

    很不幸的告诉大家...evc也不支持iostream...郁闷了...那位大哥能用file.read写个啊...急用...
      

  9.   


    HANDLE hFile; 
    DWORD dwBytesToRead, dwBytesRead;
    TCHAR * pcBuffer;
     
    hFile = ::CreateFile(TEXT("XXX.txt"),
                         GENERIC_READ,
                         FILE_SHARE_READ,
                         NULL,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         NULL);
     
    if (hFile != INVALID_HANDLE_VALUE) 
    {
    dwBytesToRead = ::GetFileSize(hFile,  NULL);
    pcBuffer = new TCHAR[dwBytesToRead];
    if (pcBuffer)
    {
    if (::ReadFile(hFile, pcBuffer, dwBytesToRead, &dwBytesRead, NULL))
    {
    // 找空格再找回车和换行符,逐行提取
    } delete [] pcBuffer;
    } ::CloseHandle(hMapFile);
    }
      

  10.   

    一次读一个字符
    判断是不是'\n'
    如果是就是一行
    这就是Readline
      

  11.   

    假设数据文件为"data.txt", 其中数据个数已知,比如为20。如果未知,也能处理。
    const num = 20;
    FILE* pf = fopen("data.txt", "r");
    int x, y;
    CPoint p[num];
    for(int i=0; i<num; i++)
    {
        fscanf(pf, "%d %d", &x, &y);
        p[i].x = x;
        p[i].y = y;
    }
    fclose(pf);
    希望对你有帮助
      

  12.   

    试试这段用集合写的代码:#include <vector>
    #include <string>std::vector <std::Point> pt;
    Point _pt;
    CString  strRead, strTemp;CStdioFile fileRead;
    if(!infile.Open(filename, CFile::modeRead))
    {
    AfxMessageBox("没有找到该文件!");
    return;
    } infile.SeekToBegin(); while(fileRead.ReadString(midFile))
    {      
                nTemp = strRead.Find(" ");
                strTemp = strRead.Left(nTemp);
                x = atoi(strTemp);
                _pt.x = x;
                strTemp  = strRead.Right(strRead.GetLength()-nTemp-1);
                y = atoi(strTemp);
                _pt.y = y;     
                pt.push_back(_pt);
             }                           
            }
      

  13.   

    #include <vector> 
    #include <string> std::vector <std::Point> pt; 
    Point _pt; 
    CString  strRead, strTemp;CStdioFile fileRead; 
    if(!fileRead.Open(filename, CFile::modeRead)) 

    AfxMessageBox("没有找到该文件!"); 
    return; 
    } fileRead.SeekToBegin(); while(fileRead.ReadString(strRead)) 
    {      
                nTemp = strRead.Find(" "); 
                strTemp = strRead.Left(nTemp); 
                x = atoi(strTemp); 
                _pt.x = x; 
                strTemp  = strRead.Right(strRead.GetLength()-nTemp-1); 
                y = atoi(strTemp); 
                _pt.y = y;    
                pt.push_back(_pt); 
     } 
      

  14.   

    CTypedPtrArray<CPtrArray ,CPoint *> pointData;CStdioFile myfile;
     CFileException fileException;
    CString pszFileName;//路径
    if(myfile.Open(pszFileName ,CFile::typeText|CFile::modeCreate|CFile::modeReadWrite), & fileException)
    {
    myfile.SeekToBegin();
    pointData.Add(new CPoint(x坐标, y坐标 ) );
    }