D
DAVE P
below is code snippet
what im trying to do is open a file and Read Bytes into a buffer find end of
line (like crlf) in this case "~"
The buffer will be the bytes up to the "~" then i need to parse the buffer
then seek back to where the "~" was
found
any comments on the below code is fine.....its not complete..but i hope you
understand what im trying to accomplish
just read bytes, move pointer, work with string, write a new file
thanks
dave
private void Fixfile()
{
FileStream strmr;
FileStream strmw;
String sEolf ='~'
int ipos;
// this.source and this.target are textboxes if source(read) File and
Target File (write)
try
{
strmr = new FileStream(this.source.Text,
FileMode.Open,
FileAccess.Read);
strmw = new FileStream(this.target.Text,
FileMode.Create,
FileAccess.ReadWrite);
int nTotBytes = 0;
int nBytes2Read;
int nBytesRead;
nBytes2Read = 1024;
string sbuffer;
string sNewBuffer;
byte[] bArray = new byte[nBytes2Read];
while (true)
{
nBytesRead = strmr.Read(bArray, 0, nBytes2Read);
cbuffer = ByteArray2String(bArray); //convert
byte array to string code below
ipos=cbuffer.IndexOf(sEol);
if (ipos>0)
// get offset in file where EOL was found
{
ntotBytes+=ipos;
//set a string to the current line
}
else
{
ntotbytes+=nBytesRead;
//set a string to buffer
}
// here i will work with the buffer then write the new
buffer to the target file below
// im just doing a write to the target at this point
strmw.Write(bArray, 0, bArray.Length);
if (nTotBytes == strmr.Length)
{
Console.WriteLine("hey now");
break;
}
}
strmr.Close();
strmw.Close();
}
catch (Exception e)
{
MessageBox.Show("Error File open");
return;
}
// Clear out any remnants in the file
// strm.SetLength (0);
/*
foreach (char ch in str1)
{
strm.WriteByte((byte)ch);
}
foreach (char ch in str2)
{
strm.WriteByte((byte)ch);
}
// Seek from the beginning of the file
strm.Seek(str1.Length, SeekOrigin.Begin);
// Read 17 bytes and write to the console.
byte[] text = new byte[17];
strm.Read(text, 0, text.Length);
ShowText(text);
// Seek back 17 bytes and reread.
strm.Seek(-17, SeekOrigin.Current);
strm.Read(text, 0, text.Length);
ShowText(text);
// Seek from the end of the file to the beginning of the second
line.
strm.Seek(-str2.Length, SeekOrigin.End);
strm.Read(text, 0, text.Length);
ShowText(text);
*/
}
static string ByteArray2String(byte[] bytearray)
{
StringBuilder str = new StringBuilder(bytearray.Length);
foreach (byte b in bytearray)
{
str.Append((char)b);
}
return (str.ToString());
}
}
what im trying to do is open a file and Read Bytes into a buffer find end of
line (like crlf) in this case "~"
The buffer will be the bytes up to the "~" then i need to parse the buffer
then seek back to where the "~" was
found
any comments on the below code is fine.....its not complete..but i hope you
understand what im trying to accomplish
just read bytes, move pointer, work with string, write a new file
thanks
dave
private void Fixfile()
{
FileStream strmr;
FileStream strmw;
String sEolf ='~'
int ipos;
// this.source and this.target are textboxes if source(read) File and
Target File (write)
try
{
strmr = new FileStream(this.source.Text,
FileMode.Open,
FileAccess.Read);
strmw = new FileStream(this.target.Text,
FileMode.Create,
FileAccess.ReadWrite);
int nTotBytes = 0;
int nBytes2Read;
int nBytesRead;
nBytes2Read = 1024;
string sbuffer;
string sNewBuffer;
byte[] bArray = new byte[nBytes2Read];
while (true)
{
nBytesRead = strmr.Read(bArray, 0, nBytes2Read);
cbuffer = ByteArray2String(bArray); //convert
byte array to string code below
ipos=cbuffer.IndexOf(sEol);
if (ipos>0)
// get offset in file where EOL was found
{
ntotBytes+=ipos;
//set a string to the current line
}
else
{
ntotbytes+=nBytesRead;
//set a string to buffer
}
// here i will work with the buffer then write the new
buffer to the target file below
// im just doing a write to the target at this point
strmw.Write(bArray, 0, bArray.Length);
if (nTotBytes == strmr.Length)
{
Console.WriteLine("hey now");
break;
}
}
strmr.Close();
strmw.Close();
}
catch (Exception e)
{
MessageBox.Show("Error File open");
return;
}
// Clear out any remnants in the file
// strm.SetLength (0);
/*
foreach (char ch in str1)
{
strm.WriteByte((byte)ch);
}
foreach (char ch in str2)
{
strm.WriteByte((byte)ch);
}
// Seek from the beginning of the file
strm.Seek(str1.Length, SeekOrigin.Begin);
// Read 17 bytes and write to the console.
byte[] text = new byte[17];
strm.Read(text, 0, text.Length);
ShowText(text);
// Seek back 17 bytes and reread.
strm.Seek(-17, SeekOrigin.Current);
strm.Read(text, 0, text.Length);
ShowText(text);
// Seek from the end of the file to the beginning of the second
line.
strm.Seek(-str2.Length, SeekOrigin.End);
strm.Read(text, 0, text.Length);
ShowText(text);
*/
}
static string ByteArray2String(byte[] bytearray)
{
StringBuilder str = new StringBuilder(bytearray.Length);
foreach (byte b in bytearray)
{
str.Append((char)b);
}
return (str.ToString());
}
}