文件读写。 [英] file to read and write.
本文介绍了文件读写。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何编写程序来读取大小为4096的文件,我必须为此提供块代码。之后通过检索这些块代码来编写文件?
我当前的代码是这个
How can I write a program to read a file of size 4096 and I have to give block code for that. After that to write the file by retrieving those block code?
My current code is this
using System;
using System.IO;
class FileRead
{
string filereadbuf;
public void ReadFile(string FileName, int FileSize)
{
char[] buf = new char[FileSize];
StreamReader sr = new StreamReader(new FileStream(FileName, FileMode.Open, FileAccess.Read));
int retval = sr.ReadBlock(buf, 0, FileSize); //
Console.Write("Total Bytes Read = " + retval + "\n");
filereadbuf = new string(buf);
Console.WriteLine(filereadbuf);
sr.Close();
}
}
class TestFileRead
{
public static void Main(string[] args)
{
String[] cmdline = Environment.GetCommandLineArgs();
Console.WriteLine("File Reader Using Stream Reader & File Stream \n");
if (cmdline.Length < 2)
{
Console.WriteLine("Usage: " + cmdline[0] + " <input file> ");
return;
}
File[] fe = (new Directory(".")).GetFiles(cmdline[1]);
if (fe.Length == 0)
{
Console.WriteLine(cmdline[1] + ": file not found");
return;
}
FileRead fr = new FileRead();
try
{
fr.ReadFile(cmdline[1], (int)fe[0].Length);
}
catch (IOException e)
{
Console.WriteLine("I/O error occured" + e);
return;
}
}
}
推荐答案
你可以查看MSDN:基本文件I / O [ ^ ]。它提供了几种常见的场景以及如何通过C#实现它们。
对于Block Code,你会发现大量的谷歌点击,例如 http://www.complextoreal.com/chapters/block.pdf [ ^ ]。
干杯
Andi
You may have a look at MSDN: Basic File I/O[^]. It provides several common scenarios and how to implement them by means of C#.
For Block Code, you find plenty of google hits, e.g. http://www.complextoreal.com/chapters/block.pdf[^].
Cheers
Andi
您的任务是实现byte [] BlockDecode(byte [] encoded)
函数。
起点可能是以下骨架。
由于到目前为止你还没有提供任何关于块代码的信息,我不能为它提供任何实现...... (参见下面的BlockDecode方法主体)。
Your task is to implement thebyte[] BlockDecode(byte[] encoded)
function.
A starting point might be the following skeleton.
Since you do not provide any information so far on the block code, I can not provide any implementation for it... (see BlockDecode method body below).
static void Main(string[] args)
{
...
string inFilePath = ...;
string outFilePath = ...;
...
byte[] encoded = File.ReadAllBytes(inFilePath);
byte[] decoded = BlockDecode(encoded);
File.WriteAllBytes(outFilePath, decoded);
}
private static byte[] BlockDecode(byte[] encoded)
{
byte[] decoded = new byte[encoded.Length];
int decodedBytes = 0;
// here you must provide the code for extracting the payload
// from the encoded bytes counting up the number of decoded bytes
...
Array.Resize(ref decoded, decodedBytes);
return decoded;
}
这篇关于文件读写。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文