473,509 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read from stream


Hello,
I would like to know how can I read a file by this way:
Read from line X to Y and in other iteration from line Y to P and on and
on. How to implement it? Which method and how? Thank u!
*** Sent via Developersdex http://www.developersdex.com ***
Dec 19 '07 #1
11 2263
On Dec 19, 7:58 am, csharpula csharp <csharp...@yahoo.comwrote:
I would like to know how can I read a file by this way:
Read from line X to Y and in other iteration from line Y to P and on and
on. How to implement it? Which method and how? Thank u!
It's not clear what you mean - what are X, Y and P? Could you describe
the issue you're trying to solve in real-world terms?

Jon
Dec 19 '07 #2
If you are talking about "lines", then you probably want a
StreamReader, via File.OpenText(path) - which has methods such as
ReadLine(). If you are just reading forwards, then this should be
quite easy - just keep track of which line you are on. You could
probably wrap it up a bit for convenience, but it is hard to say "how"
without a bit more information on what you are trying to do...

Marc
Dec 19 '07 #3

I will try to explain myself :I want to read couple of times same file
which is being updated all the time. I want to read from beggining till
end at phase A and in phaseB to read from the end of A till the new end
and so on.
How to do it? Hope it's more clear. Thanks!
*** Sent via Developersdex http://www.developersdex.com ***
Dec 19 '07 #4
csharpula csharp <cs*******@yahoo.comwrote:
I will try to explain myself :I want to read couple of times same file
which is being updated all the time. I want to read from beggining till
end at phase A and in phaseB to read from the end of A till the new end
and so on.
How to do it? Hope it's more clear. Thanks!
Well, you can find the position of a stream, then reopen the stream and
seek directly to that position, if that helps.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 19 '07 #5


"Jon Skeet [C# MVP]" wrote:
csharpula csharp <cs*******@yahoo.comwrote:
I will try to explain myself :I want to read couple of times same file
which is being updated all the time. I want to read from beggining till
end at phase A and in phaseB to read from the end of A till the new end
and so on.
How to do it? Hope it's more clear. Thanks!

Well, you can find the position of a stream, then reopen the stream and
seek directly to that position, if that helps.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
.... and hope that the buffer is flushed on complete lines by the process
that is writing the file. It could get tricky otherwise.

Dec 19 '07 #6
... and hope that the buffer is flushed on complete lines by the process
that is writing the file. It could get tricky otherwise.
and that one can read a file opend by another process... got that
problem switching from os/2 file servers to nt4 back in the days... NT
ans OS/2 share was read only for the reader but the file was locked in
nt, not in os/2, and the program doing it was bought, some old
datalogger, AAC-2 I think...

havent tried that since then in win, but got the feeling somehow that
an app like word for example locks files "harder" than notepad... not
investigated that, so it might be a problem if he cant control the
source of the file... in vms u can do a backup/ignore=interlock but
the problem with flushing the buffer (as Jon said) is still there.

can you get around that with storing data in a DB instead? then you
can use triggers and other methods (Sp to insert data only stores new
data in other table, when you read it clean it) to tell when new data
arrives too...

thats just ideas... good luck..

//CY
Dec 19 '07 #7
csharpula csharp wrote:
I will try to explain myself :I want to read couple of times same file
which is being updated all the time. I want to read from beggining till
end at phase A and in phaseB to read from the end of A till the new end
and so on.
How to do it?
Some combination of Open/Seek/Close.

Maybe the little hack attached below can get you started.

Arne

================================================

using System;
using System.IO;
using System.Threading;

namespace E
{
public class TailSniff
{
private string filename;
private long pos;
public TailSniff(string filename)
{
this.filename = filename;
pos = 0;
}
public StreamReader Read()
{
FileStream fs = new FileStream(filename, FileMode.Open,
FileAccess.Read);
long len = fs.Length;
fs.Seek(pos, SeekOrigin.Begin);
byte[] b = new byte[len - pos];
fs.Read(b, 0, b.Length);
fs.Close();
pos = len;
return new StreamReader(new MemoryStream(b));
}
}
public class MainClass
{
public static void Main(string[] args)
{
TailSniff ts = new TailSniff(@"C:\z.z");
for(;;)
{
StreamReader sr = ts.Read();
string line;
while((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
sr.Close();
Thread.Sleep(10);
}
}
}
}
Dec 20 '07 #8


Hello,
The problem is that I can't use Seek or strea.lenth in StreamReader. So
how can I do it? Thanks!

*** Sent via Developersdex http://www.developersdex.com ***
Dec 20 '07 #9
I can't use Seek or strea.lenth in StreamReader

You can seek the .BaseStream and call .DiscardBufferedData(), though

Marc
Dec 20 '07 #10

But this stream is reading stream and it throwsexception in debug if I
use Seek with Stream. Any other ideas? Thansk a lot!
*** Sent via Developersdex http://www.developersdex.com ***
Dec 20 '07 #11
You might try something like this:

static FileStream FS;
static StreamReader SR;
static int CharsRead;
static char[] buff;

public static void GetText()
{
int count = 0;
if (CharsRead 1) SR.BaseStream.Position = CharsRead - 1;
do
{
count = SR.ReadBlock(buff, 0, 2048);
if (count 0)
{
CharsRead += count;
string st = new string(buff);
if (count < 2048) st = st.Substring(0, count);
Console.WriteLine(st);
}
} while (SR.Peek() != -1);
}

static void Main(string[] args)
{
buff = new char[2048];
FS = new FileStream("D:\\testtext.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
SR = new StreamReader(FS);
GetText();
Console.WriteLine ("Type 'quit' and ENTER to quit");
while (Console.ReadLine() != "quit")
{
GetText();
Console.WriteLine("Type 'quit' and ENTER to quit");
}
SR.Close();

Dec 20 '07 #12

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

Similar topics

6
7343
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
16
3460
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
2
4048
by: GB | last post by:
Hi Everybody! I have 2 different processes/application. One is writing to a file and another is reading from it. For some reason the code doesnt seems to work and gives mscorlib.dll IOException...
14
19012
by: Laszlo Szijarto | last post by:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory stream or even file stream in big endian order or do I have to write something custom to reverse the byte order? So, for...
2
12318
by: Just D. | last post by:
All! How should we read any file from some URL? I found in MSDN the method URLDownloadToFile function, but it's for C#. There is another example to read but it doesn't work if the page is more...
1
2986
by: eiji | last post by:
Hi folks, I'm new to binary-file handling and try to work/learn playing around with MD5. Now everything works fine with small files, but when files get bigger(e.g. 28MB) this loop stops in the...
2
4487
by: Brent Rogers | last post by:
I am having trouble with the DeflateStream.Read() method. For some reason it wants to return Zero. I have this set of classes: =================== using System; using...
2
6599
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
63
3182
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
3
11473
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I'm creating an application that will read emails from GMail, using the System.Net.Sockets.TcpClient and POP protocol. However, I am having a problem with my SslStream. ...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
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
7344
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,...
1
7069
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
7505
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
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.