vc按行读文件,如何读取每行的从指定位置开始到指定位置结束的字符啊,比如我的文件内容如下0000  00 e0 fc 65 72 4e 00 14 22 60 c9 69 08 00 45 00   ...erN.."`.i..E.
0010  01 48 11 d6 00 00 80 11 e2 32 0a a4 18 54 0a a4   .H.......2...T..
0020  18 01 00 44 00 43 01 34 e8 75 01 01 06 00 c5 45   ...D.C.4.u.....E
0030  da 72 0b 00 80 00 0a a4 18 54 00 00 00 00 00 00   .r.......T......
0040  00 00 00 00 00 00 00 14 22 60 c9 69 00 00 00 00   ........"`.i....我想读从第7个字节开始到48字节位置,最后得到00 e0 fc 65 72 4e 00 14 22 60 c9 69 08 00 45 00   
01 48 11 d6 00 00 80 11 e2 32 0a a4 18 54 0a a4
18 01 00 44 00 43 01 34 e8 75 01 01 06 00 c5 45 
00 00 00 00 00 00 00 14 22 60 c9 69 00 00 00 00 我的代码如下  CStringArray   strarray;   
  CStdioFile   file;   
  if(   !file.Open("3.txt",   CFile::modeRead)   )   
  {   
  AfxMessageBox("can   not   open   file!");   
  return;   
  }   
  CString   strLine;   
  while(file.ReadString(strLine))   //这里面如何取第7个字节开始到48字节的内容,赋值给下面的字符串数组啊
  {   
  strarray.Add(strLine);   
  }     file.Close();  请高手指教啊

解决方案 »

  1.   

    while(file.ReadString(strLine))  
    只能读取一行后你自己再从strLine里截取
      

  2.   

    你这文件里不是字符串,不能用ReadString和CStringArray,直接存在一个buffer里就行了啊。
    unsigned char temp[100];
      CStdioFile  file; 
      if(  !file.Open("3.txt",  CFile::modeRead)  ) 
      { 
      AfxMessageBox("can  not  open  file!"); 
      return; 
      } 
    else
    {
    file.Seek(7, CFile::begin);
    file.Read(temp, 42);
    file.Close();
     
      }
      

  3.   

    这个要用到一个函数, 
    DWORD WINAPI SetFilePointer(
      __in         HANDLE hFile,
      __in         LONG lDistanceToMove,
      __inout_opt  PLONG lpDistanceToMoveHigh,
      __in         DWORD dwMoveMethod
    );
    // Case One: calling the function with lpDistanceToMoveHigh == NULL 
     
    // Try to move hFile file pointer some distance  
    dwPtr = SetFilePointer (hFile, lDistance, NULL, FILE_BEGIN) ; 
     
    if (dwPtr == INVALID_SET_FILE_POINTER) // Test for failure

        // Obtain the error code. 
        dwError = GetLastError() ; 
     
        // Deal with failure 
        // . . . 
     
    } // End of error handler 
    // 
    // Case Two: calling the function with lpDistanceToMoveHigh != NULL 
     
    // Try to move hFile file pointer a huge distance 
    dwPtrLow = SetFilePointer (hFile, lDistLow, & lDistHigh, FILE_BEGIN); 
     
    // Test for failure
    if (dwPtrLow == INVALID_SET_FILE_POINTER && 
       (dwError = GetLastError()) != NO_ERROR )

        // Deal with failure 
        // . . . } // End of error handler
      

  4.   

    按照我这个例子,该函数怎么调用啊,
    我象下面这样调用出错啊
    CStringArray   strarray;   
      CStdioFile   file;   
      if(   !file.Open("3.txt",   CFile::modeRead)   )   
      {   
      AfxMessageBox("can   not   open   file!");   
      return;   
      }   
      CString   strLine;   
      while(file.ReadString(strLine))   
      {    strarray.Add(strLine.Mid(7,48-7);   //这样调用为何不行啊
      }   
      file.Close();   
     
    请wwwllg,mengxianbo921帮忙看看啊
      

  5.   

    请问各位高手,我采用这样的方法:从0个开始读就可以
    CStringArray  strarray;  
      CStdioFile  file;  
      if(  !file.Open("3.txt",  CFile::modeRead)  )  
      {  
      AfxMessageBox("can  not  open  file!");  
      return;  
      }  
      CString  strLine;  
      while(file.ReadString(strLine))  
      {    strarray.Add(strLine.Mid(0,48-7));  //从0个开始读
      }  
      file.Close();  
    而从第7个开始读为什么就报错呢
    CStringArray  strarray;  
      CStdioFile  file;  
      if(  !file.Open("3.txt",  CFile::modeRead)  )  
      {  
      AfxMessageBox("can  not  open  file!");  
      return;  
      }  
      CString  strLine;  
      while(file.ReadString(strLine))  
      {    strarray.Add(strLine.Mid(7,48-7));  //从第7个开始读
      }  
      file.Close();  
      

  6.   

    用fseek,移动文件指针到7字节的地方,然后用fread读指定长度的字节.
    这样不行么?
      

  7.   

    strLine.Mid(7,48-7)pls confirm the length of strLine is >=7,or it would throw an exception
      

  8.   

    ok ,由于我文件中存在空白行,所以strarray.Add(strLine.Mid(7,48-7));才会出问题,先将空白行删除,再调用Mid函数,该问题解决了。
    感谢wwwllg,mengxianbo921,jourbin