使用FileStream流读取任何文件为二进制数组。即可保存至Byte[]类型的变量中。
string 郑神path = Server.MapPath("/test.txt")FileStream fs = new FileStream(path, FileMode.Open)
//获取文件大小
long size = fs.Length
byte[] array = new byte[size]
//将文件读到byte数组中
亏悉 fs.Read(array, 0, array.Length)
fs.Close()
上述代码就示例了如何用FileStream读取一个文件至销丛乎Byte[]类型的变量中。
/// <summary>/// 将文件转换成byte[] 数组
/// </summary>
/// <param name="fileUrl">文件路径文件名称</param>
/// <returns>byte[]</returns>
protected byte[] GetFileData(string fileUrl)
{
FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read)
try
{
byte[] buffur = new byte[fs.Length]
fs.Read(buffur, 0, (int)fs.Length)
return buffur
}
雹迅 catch (Exception ex)
{
//MessageBoxHelper.ShowPrompt(ex.Message)
return null
}
finally
{
if (fs != null)
{
//关闭资源
fs.Close()
}
}
}
/// <summary>
/// 将文件转换成byte[] 数组源猜此
/// </summary>
/// <param name="fileUrl">文件路径文件兆纤名称</param>
/// <returns>byte[]</returns>
protected byte[] AuthGetFileData(string fileUrl)
{
using (FileStream fs = new FileStream(fileUrl, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
byte[] buffur = new byte[fs.Length]
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(buffur)
bw.Close()
}
return buffur
}
}
帮你写了个简棚谨闹单的你看看就知道怎么写入了:)#include"stdio.h"#define MAX 1000
main()
{FILE *fp
int i=0
char sky[MAX]
printf("please input:\n>>")
gets(sky)
fp=fopen("001.txt","w")
while(sky[i]!='\0'链罩晌祥 )
{fprintf(fp,"%c",sky[i])
i++
}
fclose(fp)
printf("write over!")
getch()}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)