内容を読み込んだ後、テキストファイルの最後の行を取り除きたい。テキストファイルはかなり大きいので、パフォーマンス上の問題のため、読み書きはオプションではありません。C言語でtxtファイルを 'x'バイトで切り捨てる
私の現在の考えは、最後の行が(キャリーリターンと共に)表すバイト数を計算し、ファイルを切り捨てることです。
「Filestream.setLength()」を使用して参照したオプションの多くは、これがどのように動作するのか混乱しています。
「読み込み」機能がバイトを読み込んでバッファに書き戻すので、ファイルを書き戻すのではなく、特定のバイト数でファイルを停止しますか?または、私が読んでいるときにこの関数を使用して、テキストファイルの「終わりの位置」を例えば24バイトに戻すことができますか?
これは、ストリームのサイズを変更すると、それだけ左バイトを指すように単にファイルテーブルのインデックスを書き換えます私は
try
{
//reading
using (StreamReader reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
//gets the line
line = reader.ReadLine();
if (!line.StartsWith("KeyWord", StringComparison.InvariantCultureIgnoreCase))
{
//add number of lines
lineCount += 1;
}
else
{
//Finds the occurence of the first numbers in a string
string resultString = Regex.Match(line, @"\d+").Value;
long lastLineBytes = 0;
foreach (char c in line)
{
//each char takes up 1 byte
lastLineBytes++;
}
//carriage return
lastLineBytes += 2;
long fileLength = new FileInfo(filePath).Length;
Trace.WriteLine("The length of the file is " + fileLength);
//the size of the file - the last line
//truncate at this byte position, and we will be done.
long newFileLength = fileLength - lastLineBytes;
//Truncation goes ehre
}
}
}
}
catch (Exception e)
{
throw e;
}
はhttps://msdn.microsoft.com/en-us/library/system.io.filestream.setlength.aspx – SLaks
私はこれを読んで、私はまだ問題私はこれを使用することができますどのように理解を持っています練習。私がチェックアウトできる使用例がありますか? – Childishforlife
私は確信できませんが、ストリームのサイズを変更すると、ファイルテーブルのインデックスが左バイトだけに変わるだけで、本当にシンプルなテストプログラムが作成され、テストするのがベストチャンスです。 – Gusman