473,396 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File handling

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());
}
}
Jan 23 '07 #1
2 2148
DAVE P wrote:
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
You shouldn't be using streams directly when working with text -
streams are for binary data, and you use readers/writers to read/write
text. StreamReader provides the conversion between the two,
effectively.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.

Jon

Jan 23 '07 #2
Thanks for the Response
dave

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
DAVE P wrote:
>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

You shouldn't be using streams directly when working with text -
streams are for binary data, and you use readers/writers to read/write
text. StreamReader provides the conversion between the two,
effectively.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.

Jon

Jan 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
5
by: John Douglass | last post by:
I'm fairly new to doing involved file i/o and I came across something weird with a program I'm writing (modifying MIDI data, if it makes any difference). With some input files, when I modify data...
8
by: Gabe Moothart | last post by:
Hi, I'm writing a windows service which interacts with a separate process. Basically, it calls a process which creates a file, and then my service reads that file. The problem is, the external...
14
by: Al Smith | last post by:
I need help in implementing proper error handling. I am trying to upload a file based on the sample code below. The code works well except if the file selected is too big. I do know about the...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
5
by: Digital Puer | last post by:
I fixed a bug today that went against my intuition. I am on Linux. I had a class that fopen'ed some files. When I called delete on these objects, I expected that the files would be closed...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
5
by: kailashchandra | last post by:
I am trying to upload a file in php,but it gives me error msg please Help me? My Code is like below:- i have one php file named upload.php and i have another html file named upload.html and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.