Reading only part of a file in between string markers.

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
Is there a way that I can put special start and end marks in my text files that I can use to have a stream read in between them?
Ex:

:TB1S:jglkaj dgjlkajd akjdlg kjdglkaj glajsd:TB1E:
 
The link was all vb stuff. I need C#. Also, I have no idea what regular expressions are or how to use them. Please direct me somewhere or explain if you can.
 
Why not try Google? There are loads of articles on Regular Expressions online - what they are, how to use them, and using them with the .NET framework.
 
If youve got the ability to define these start and end markers, why not use xml? Then you dont have to figure out your own parsing logic, just build on what the framework gives you. just a thought.
 
I was so focused on the question itself that I didnt consider it,
as quwiltw mentioned XML is really your best route.
 
With xml can I append strings into certain markers in the xml document just as I was going to do with with my text document, except easier?
 
So, it would be kind of like an array except it is all text. In other words I can reference a certain node from the program and write to and extract data. Right?
 
Yes, or you could Serialize a class to and from XML, and then all
youd need to do is access the class members. Or you could
create an XML Schema and then load and save the XML through a
DataSet.

The possibilities are endless. If youre looking to save and load
program settings, or something similar, Serialization is the way to go.
 
Runtime Error : Binary Formatter incompatability. Expected version - 1.0, received version - (estemate) 17654837.83746353

Code:
this.NewLogB.Enabled=true; NLClosed=0;
					string file="CommLog\\"+DateTime.Now.ToLongDateString()+".txt";
					this.richTextBox1.Clear();
					//StreamReader SR=new StreamReader(file);
					//this.richTextBox1.Text+=SR.ReadToEnd();
					string[]holdIt=new string[20];
					FileStream FS=new FileStream(file,FileMode.Open);
					BinaryFormatter BF=new BinaryFormatter();


(this is the problem)::::::
holdIt=(string[])BF.Deserialize(FS);
					FS.Close();
					richTextBox2.Clear();
					richTextBox1.Text+=holdIt[0];
					richTextBox2.Text+=holdIt[1];

What did I do wrong?
 
If youre serializing to binary, then it wont be a human-readable
textfile in the end. Here is an example of serializing to XML:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sa() As String = {"Extreme", "Visual", "Basic", "Forum"}
        Dim ser As New Xml.Serialization.XmlSerializer(GetType(String()))
        Dim fs As New IO.FileStream( _
            Application.StartupPath & "\beh.txt", IO.FileMode.OpenOrCreate)

        Dim tw As New IO.StreamWriter(fs)
        Dim tr As New IO.StreamReader(fs)

        ser.Serialize(tw, sa)

        Dim dum As String, ret() As String, out As String
        ret = ser.Deserialize(tr)

        For Each dum In ret
            out &= dum & " "
        Next

        MessageBox.Show(out.Trim)
    End Sub
There is lots of documentation in the MSDN regarding
serialization, when to use it, and how to use it. I would suggest giving
it a good once-over. The Xml.Serialization namespace is the key to
XML serialization, so check out the docs on it.
 
That is vb not c# and when I save a string array in binary format I can read what I wrote in between the squares. But even if I couldnt, cant I deserialize it to read it?

Remember, these binary text files that I am making are only for use within the application. I would be a bad thing if the user had access to the text files. Then they could edit them. But I should be able to deserialize the text files in my application and read them just as they were written, right?
 
Last edited by a moderator:

Similar threads

C
Replies
0
Views
63
Cas Raj
C
S
Replies
0
Views
57
sva0008
S
I
Replies
0
Views
52
It_s Meee
I
Back
Top