2012-12-26 3 views
2

으로 변환하므로 파일을 읽기 위해 kernel32에서 ReadFile을 사용하고 있습니다. 다음은 SetFilePointerReadFile의 도움으로 파일을 읽는 코드입니다. 나는이 기능을 사용하면(readfile에서) 바이트 배열을

public long ReadFileMe(IntPtr filehandle, int startpos, int length, byte[] outdata) 
{ 
    IntPtr filea = IntPtr.Zero; 
    long ntruelen = GetFileSize(filehandle, filea); 
    int nRequestStart; 
    uint nRequestLen; 
    uint nApproxLength; 
    int a = 0; 
    if (ntruelen <= -1) 
    { 
     return -1; 
    } 
    else if (ntruelen == 0) 
    { 
     return -2; 
    } 

    if (startpos > ntruelen) 
    { 
     return -3; 
    } 
    else if (length <= 0) 
    { 
     return -5; 
    } 
    else if (length > ntruelen) 
    { 
     return -6; 
    } 
    else 
    { 
     nRequestStart = startpos; 
     nRequestLen = (uint)length; 

     outdata = new byte[nRequestLen - 1]; 

     SetFilePointer(filehandle, (nRequestStart - 1), ref a, 0); 
     ReadFile(filehandle, outdata, nRequestLen, out nApproxLength, IntPtr.Zero); 
     return nApproxLength; //just for telling how many bytes are read in this function 
    } 
} 

, 그것은 (다른 용도로) 작동하므로이 코드를 테스트하고 작품이다.

하지만 주된 문제는이 함수가 바이트를 string에 넣는 매개 변수로 outdata을 변환해야한다는 것입니다.

Encoding.Unicode 등 (모두 UTF)을 사용해 보았지만 작동하지 않습니다.

+0

시도해 보셨습니까? string result = System.Text.Encoding.Default.GetString (outdata); 출력을 문자열로 변환 할 수 있는지 확인하십시오. – Saravanan

+0

나는 그것을 시도했다. 아무런 결과도주지 않습니다 .. – newbie

+0

반환 매개 변수 또는 참조 매개 변수가 없으면 함수는 아무 것도 넣지 않습니다 ... – knaki02

답변

1

흠 ... Encoding.Name_of_encoding.GetString은 일을해야 ... 시도의이 같은 떨어지게 :

var convertedBuffer = Encoding.Convert(
      Encoding.GetEncoding(/*name of encoding*/),Encoding.UTF8, outdata); 
var str = Encoding.UTF8.GetString(convertedBuffer); 

UPDATE :이 약 와 무슨 일이? :

using (var streamReader = new StreamReader(@"C:\test.txt", true)) 
     { 
      var currentEncoding = streamReader.CurrentEncoding.EncodingName; 
      Console.WriteLine(currentEncoding); 
     } 
+0

네. 인코딩의 이름은 내가 찾고있는 것입니다. < – newbie

+0

그것은 유니 코드 (UTF-8)에 있다고 말합니다. 그러나 나는 유니 코드와 utf-8을 시도했다. – newbie

0

당신 outdata 매개 변수에 out 매개 변수를 추가해야 할 수도 있습니다. Passing Arrays Using ref and out

public long ReadFileMe(IntPtr filehandle, int startpos, int length, out byte[] outdata)