文本文件数据格式如下:
   618000.000
    97850.000
       200
       108
        10
724.972       724.416       723.835       725.857       726.499       727.245       727.974       728.644       729.191       729.567       730.760       733.517       736.169       737.416       739.092       740.852       741.709       740.721       738.112       733.909       729.570       725.597       721.568       717.695       715.769       714.489       713.410       711.986       710.192       706.526       701.346       698.279       695.278       694.486       699.540       708.693       720.687       734.957       746.299       756.114       762.901       771.490       777.640       781.532       784.810       786.525       787.370       788.216       789.746       791.962       794.740       797.701       800.203       800.829       801.528       803.421       809.182       814.195前面我想用5个变量如a,b,c,d,e分别储存618000.000,97850.000,200,108,10,后面的我想只用一个变量t储存.
我写的内存文件映射代码:HANDLE hFile=CreateFile(file,
       GENERIC_READ,
       0,
       NULL,
       OPEN_EXISTING,
       FILE_ATTRIBUTE_NORMAL,
       NULL);   if (hFile==INVALID_HANDLE_VALUE)
        {
            return(FALSE);
        }
   HANDLE hFileMapping=CreateFileMapping(hFile,
       NULL,
       PAGE_READONLY,
       0,
       0,
       NULL);
   
   if (hFileMapping==NULL)
        {
            CloseHandle(hFile);
        }   PVOID pvFile=MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0);   if (pvFile==NULL)
        {
            CloseHandle(hFileMapping);
            CloseHandle(hFile);
            return(FALSE);
        }  ASSERT(pvFile);
  double *dd=(int *)pvFile;
  再往下读取文件该怎么写?如已知数据结构的文本文件和没有数据结构的文本文件。
请高手指教!谢谢。

解决方案 »

  1.   

    是把pvFile看作字符串,以空格为分隔符把各数据分开,然后用atoi或者atof之类的函数进行转换.
      

  2.   

    按照你的思路定一个结构体,typedef struct ....
    然后直接转换即可!
      

  3.   

    谢谢各位了!!下午试了一下这个数据格式,可就是得不到x y z的值。
    54712.800 56061.080 00000.000 34713.800 56061.820 00000.000 14708.300 76065.530 00000.000 
    14686.600 56062.890 00000.000 14691.700 56059.550 00000.000 14692.600 56065.470 00000.000 
    14691.700 56059.550 00000.000 14694.500 56058.480 00000.000 14696.300 56065.170 00000.000   float *dd=(float *)pvFile;
      
     
    x1=*dd;
    y1=*(dd+9);
    z1=*(dd+9);
    x2=*(dd+9);
    y2=*(dd+9);
    z2=*(dd+9);
    x3=*(dd+9);
    y3=*(dd+9);
    z3=*(dd+9);
    什么问题?
      

  4.   

    float *dd=(float *)pvFile;
      
     
    x1=*dd;dd+9;
    y1=*dd;dd+9;
    z1=*dd;dd+9;
    x2=*dd;dd+9;
    y2=*dd;dd+9;
    z2=*dd;dd+9;
    x3=*dd;dd+9;
    y3=*dd;dd+9;
    z3=*dd;
      

  5.   

    谢谢Lightest(一闪而过) ,可我按你的提示试了,还是不行,能帮我看一下代码吗?void CView::OnFileOpen() 
    {
    // TODO: Add your command handler code here  CString filename,pathname;
         CFileDialog dlg(true,_T("*"),_T("*.*"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("文本文件(*.*)|*.*|")); dlg.m_ofn.lpstrTitle = _T("读取采集数据");  
    if(IDOK==dlg.DoModal())
    { CString file=dlg.GetPathName(); float x1,x2,x3,y1,y2,y3,z1,z2,z3;   HANDLE hFile=CreateFile(file,
           GENERIC_READ|GENERIC_WRITE,
           0,
           NULL,
           OPEN_EXISTING,
           FILE_ATTRIBUTE_NORMAL,
           0);   if (hFile==INVALID_HANDLE_VALUE)
            {
           AfxMessageBox("无法创建内存映射文件!", MB_OK | MB_ICONSTOP);     
       //return(FALSE);
            }   HANDLE hFileMapping=CreateFileMapping(hFile,
           NULL,
           PAGE_READWRITE,
           0,
           0,
           NULL);
       
      if (hFileMapping==NULL)
            {
             AfxMessageBox("无法创建内存映射文件!", MB_OK | MB_ICONSTOP);   
         CloseHandle(hFile);
            }   float *pvFile;   pvFile=(float*)MapViewOfFile(hFileMapping,FILE_MAP_READ|FILE_MAP_WRITE,0,0,0);   if (pvFile==NULL)
            {
                AfxMessageBox("无法创建内存映射文件!", MB_OK | MB_ICONSTOP); 
            CloseHandle(hFileMapping);
                CloseHandle(hFile);        }
       float *dd=(float *)pvFile;
      
     
    x1=*dd;dd+9;
    y1=*dd;dd+9;
    z1=*dd;dd+9;
    x2=*dd;dd+9;
    y2=*dd;dd+9;
    z2=*dd;dd+9;
    x3=*dd;dd+9;
    y3=*dd;dd+9;
    z3=*dd;  
     
    OnDrawTriangles(x1,y1,z1,x2,y2,z2,x3,y3,z3);
                UnmapViewOfFile(pvFile);
                CloseHandle(hFileMapping);
                CloseHandle(hFile);
                 
    }
    }
      

  6.   

    API记事本读文本文件的例子~,类似,先读到缓存,然后就指针定位取数据嘛~~BOOL PopFileRead (HWND hwndEdit, PTSTR pstrFileName)
    {
         BYTE   bySwap ;
         DWORD  dwBytesRead ;
         HANDLE hFile ;
         int    i, iFileLength, iUniTest ;
         PBYTE  pBuffer, pText, pConv ;         
         if (INVALID_HANDLE_VALUE == 
                   (hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ,
                                        NULL, OPEN_EXISTING, 0, NULL)))
              return FALSE ;                    
         iFileLength = GetFileSize (hFile, NULL) ; 
         pBuffer = malloc (iFileLength + 2) ;     
         ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ;
         CloseHandle (hFile) ;
         pBuffer[iFileLength] = '\0' ;
         pBuffer[iFileLength + 1] = '\0' ;
         iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ;
         
         if (IsTextUnicode (pBuffer, iFileLength, &iUniTest))
         {
              pText = pBuffer + 2 ;
              iFileLength -= 2 ;          if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
              {
                   for (i = 0 ; i < iFileLength / 2 ; i++)
                   {
                        bySwap = ((BYTE *) pText) [2 * i] ;
                        ((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ;
                        ((BYTE *) pText) [2 * i + 1] = bySwap ;
                   }
              }                        pConv = malloc (iFileLength + 2) ;
    #ifndef UNICODE
              WideCharToMultiByte (CP_ACP, 0, (PWSTR) pText, -1, pConv, 
                                   iFileLength + 2, NULL, NULL) ;#else
              lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
    #endif     }
         else      
         {
              pText = pBuffer ;                        pConv = malloc (2 * iFileLength + 2) ;              #ifdef UNICODE
              MultiByteToWideChar (CP_ACP, 0, pText, -1, (PTSTR) pConv, 
                                   iFileLength + 1) ;        
    #else
              lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
    #endif
         }
         
         SetWindowText (hwndEdit, (PTSTR) pConv) ;
         free (pBuffer) ;
         free (pConv) ;
       
         return TRUE ;
    }BOOL PopFileWrite (HWND hwndEdit, PTSTR pstrFileName)
    {
         DWORD  dwBytesWritten ;
         HANDLE hFile ;
         int    iLength ;
         PTSTR  pstrBuffer ;
         WORD   wByteOrderMark = 0xFEFF ;     
         if (INVALID_HANDLE_VALUE == 
                   (hFile = CreateFile (pstrFileName, GENERIC_WRITE, 0, 
                                        NULL, CREATE_ALWAYS, 0, NULL)))
              return FALSE ;     
         iLength = GetWindowTextLength (hwndEdit) ;
         pstrBuffer = (PTSTR) malloc ((iLength + 1) * sizeof (TCHAR)) ;
         
         if (!pstrBuffer)
         {
              CloseHandle (hFile) ;
              return FALSE ;
         }
    #ifdef UNICODE
         WriteFile (hFile, &wByteOrderMark, 2, &dwBytesWritten, NULL) ;
    #endif     
         GetWindowText (hwndEdit, pstrBuffer, iLength + 1) ;
         WriteFile (hFile, pstrBuffer, iLength * sizeof (TCHAR), 
                    &dwBytesWritten, NULL) ;
         
         if ((iLength * sizeof (TCHAR)) != (int) dwBytesWritten)
         {
              CloseHandle (hFile) ;
              free (pstrBuffer) ;
              return FALSE ;
         }
         
         CloseHandle (hFile) ;
         free (pstrBuffer) ;
         
         return TRUE ;
    }
      

  7.   

    问题就在这里,我对指针在MapViewOfFile这个函数里的使用不太清楚,具体偏移位的计算不太懂,请高手帮我看看我自己的这段代码!
      

  8.   

    你事实上打开的是一个文本文件,应该先将文本文件读入内存,一条一条的读取字符串数据,用atof转换成为float.