欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#通過指針讀取文件的方法

 更新時間:2015年06月29日 09:01:12   作者:pythoner  
這篇文章主要介紹了C#通過指針讀取文件的方法,涉及C#針對文件的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了C#通過指針讀取文件的方法。分享給大家供大家參考。具體如下:

// readfile.cs
// 編譯時使用:/unsafe
// 參數(shù):readfile.txt
// 使用該程序讀并顯示文本文件。
using System;
using System.Runtime.InteropServices;
using System.Text;
class FileReader
{
  const uint GENERIC_READ = 0x80000000;
  const uint OPEN_EXISTING = 3;
  IntPtr handle;
  [DllImport("kernel32", SetLastError=true)]
  static extern unsafe IntPtr CreateFile(
    string FileName,        // 文件名
    uint DesiredAccess,        // 訪問模式
    uint ShareMode,          // 共享模式
    uint SecurityAttributes,    // 安全屬性
    uint CreationDisposition,    // 如何創(chuàng)建
    uint FlagsAndAttributes,    // 文件屬性
    int hTemplateFile        // 模板文件的句柄
    );
  [DllImport("kernel32", SetLastError=true)]
  static extern unsafe bool ReadFile(
    IntPtr hFile,          // 文件句柄
    void* pBuffer,        // 數(shù)據(jù)緩沖區(qū)
    int NumberOfBytesToRead,  // 要讀取的字節(jié)數(shù)
    int* pNumberOfBytesRead,    // 已讀取的字節(jié)數(shù)
    int Overlapped        // 重疊緩沖區(qū)
    );
  [DllImport("kernel32", SetLastError=true)]
  static extern unsafe bool CloseHandle(
    IntPtr hObject // 對象句柄
    );
  public bool Open(string FileName)
  {
    // 打開現(xiàn)有文件進(jìn)行讀取
    handle = CreateFile(
      FileName,
      GENERIC_READ,
      0, 
      0, 
      OPEN_EXISTING,
      0,
      0);
    if (handle != IntPtr.Zero)
      return true;
    else
      return false;
  }
  public unsafe int Read(byte[] buffer, int index, int count) 
  {
    int n = 0;
    fixed (byte* p = buffer) 
    {
      if (!ReadFile(handle, p + index, count, &n, 0))
        return 0;
    }
    return n;
  }
  public bool Close()
  {
    // 關(guān)閉文件句柄
    return CloseHandle(handle);
  }
}
class Test
{
  public static int Main(string[] args)
  {
    if (args.Length != 1)
    {
      Console.WriteLine("Usage : ReadFile <FileName>");
      return 1;
    }
    if (! System.IO.File.Exists(args[0]))
    {
      Console.WriteLine("File " + args[0] + " not found."); 
      return 1;
    }
    byte[] buffer = new byte[128];
    FileReader fr = new FileReader();
    if (fr.Open(args[0]))
    {
      // 假定正在讀取 ASCII 文件
      ASCIIEncoding Encoding = new ASCIIEncoding();
      int bytesRead;
      do 
      {
        bytesRead = fr.Read(buffer, 0, buffer.Length);
        string content = Encoding.GetString(buffer,0,bytesRead);
        Console.Write("{0}", content);
      }
      while ( bytesRead > 0);
      fr.Close();
      return 0;
    }
    else
    {
      Console.WriteLine("Failed to open requested file");
      return 1;
    }
  }
}

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

  • 詳解c#與python的交互方式

    詳解c#與python的交互方式

    這篇文章主要介紹了詳解c#與python的交互方式,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • 淺談C# 中的委托和事件

    淺談C# 中的委托和事件

    本篇文章主要介紹C# 中的委托和事件,委托和事件在 .Net Framework中的應(yīng)用非常廣泛,有興趣的可以了解一下。
    2016-12-12
  • C# mysql 插入數(shù)據(jù),中文亂碼的解決方法

    C# mysql 插入數(shù)據(jù),中文亂碼的解決方法

    用C#操作mysql時, 插入數(shù)據(jù)中文都是亂碼,只顯示問號,數(shù)據(jù)庫本身使用的是utf-8字符
    2013-10-10
  • C# 實現(xiàn)Distinct將對象按條件去重

    C# 實現(xiàn)Distinct將對象按條件去重

    這篇文章主要介紹了C# 實現(xiàn)Distinct將對象按條件去重,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C#基礎(chǔ)之Lambda表達(dá)式用法實例教程

    C#基礎(chǔ)之Lambda表達(dá)式用法實例教程

    這篇文章主要介紹了C#中Lambda表達(dá)式用法,并與之前所述的匿名方法做一比較,詳細(xì)的講述了Lambda表達(dá)式的定義及具體用法,需要的朋友可以參考下
    2014-09-09
  • 基于C#調(diào)用OCX控件的常用方法(推薦)

    基于C#調(diào)用OCX控件的常用方法(推薦)

    下面小編就為大家分享一篇基于C#調(diào)用OCX控件的常用方法推薦,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#遍歷集合與移除元素的方法

    C#遍歷集合與移除元素的方法

    這篇文章主要介紹了C#遍歷集合與移除元素的方法,結(jié)合實例形式分析了C#使用for循環(huán)遍歷集合以及add與Remove方法進(jìn)行元素添加與移除的使用技巧,需要的朋友可以參考下
    2016-06-06
  • c#互斥鎖Mutex類用法介紹

    c#互斥鎖Mutex類用法介紹

    本文詳細(xì)講解了c#互斥鎖Mutex類的用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • C#實現(xiàn)的Excel文件操作類實例

    C#實現(xiàn)的Excel文件操作類實例

    這篇文章主要介紹了C#實現(xiàn)的Excel文件操作類,結(jié)合具體實例形式分析了C#數(shù)據(jù)庫及Excel文件相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • C#流程控制詳解

    C#流程控制詳解

    這篇文章主要介紹了C#流程控制詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07

最新評論