C#通過(guò)指針讀取文件的方法
更新時(shí)間:2015年06月29日 09:01:12 作者:pythoner
這篇文章主要介紹了C#通過(guò)指針讀取文件的方法,涉及C#針對(duì)文件的相關(guān)操作技巧,需要的朋友可以參考下
本文實(shí)例講述了C#通過(guò)指針讀取文件的方法。分享給大家供大家參考。具體如下:
// readfile.cs
// 編譯時(shí)使用:/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, // 訪(fǎng)問(wèn)模式
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 // 對(duì)象句柄
);
public bool Open(string FileName)
{
// 打開(kāi)現(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;
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#基于面向過(guò)程計(jì)算加權(quán)平均分的方法
這篇文章主要介紹了C#基于面向過(guò)程計(jì)算加權(quán)平均分的方法,涉及C#數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
C#設(shè)計(jì)模式之觀(guān)察者模式實(shí)例講解
這篇文章主要介紹了C#設(shè)計(jì)模式之觀(guān)察者模式實(shí)例講解,本文詳細(xì)講解了觀(guān)察者模式的定義、優(yōu)缺點(diǎn)、代碼實(shí)例等,需要的朋友可以參考下2014-10-10
C# 開(kāi)發(fā)(創(chuàng)藍(lán)253)手機(jī)短信驗(yàn)證碼接口的實(shí)例
下面小編就為大家分享一篇C# 開(kāi)發(fā)(創(chuàng)藍(lán)253)手機(jī)短信驗(yàn)證碼接口的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
C#開(kāi)發(fā)Windows UWP系列之布局面板RelativePanel
這篇文章介紹了C#開(kāi)發(fā)Windows UWP系列之布局面板RelativePanel,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
WPF+Canvas實(shí)現(xiàn)平滑筆跡的示例代碼
這篇文章主要介紹了如何利用WPF+Canvas實(shí)現(xiàn)平滑筆跡效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-09-09

