Difficult Question: Read/Modify file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Thanks in advance for reading this.

Let's say I have a file (file01) with this data in ASCII (ignore line col):

line01 123abc
line02 Header01 Starts blah var
line03 detail01 000001
line04 detail02 000002
line05 detail03 000003
line06 detail04 000004
line07 detail05 000005
line08 detail06 000006
line09 detail07 000007
line10 Header01 Ends
line11 Header02 Starts blah var
line12 detail01 000001
line13 detail02 000002
line14 detail03 000003
line15 detail04 000004
line16 detail05 000005
line19 Header03 Ends

Here's what I need to do:

1. traverse each line and modify some section of the line
(it could postion 10-20) according to some logic in a function.

2. For Header line, I need to know the last detail line for that header
of a section (it could postion 20-25). So for Header02 (line11) I would
need to grab the value '000005' (line16).

The result for file01 will have it's line modified accordingly.

The main question is how can I traverse down line by line, and
when I get to the Header, somehow get the value of the last detail line
for that header, and continue the next line after the header which
is the first detail line.

I know you can do this in C by creating a dummy pointer to point
to the header and then traverse to the last detail line and return the
value. And then destroy the pointer.

I just don't know how to do this is .NET.

Thanks in advance for your replies!
 
ank2go said:
Thanks in advance for reading this.

Let's say I have a file (file01) with this data in ASCII (ignore line
col):

line01 123abc
line02 Header01 Starts blah var
line03 detail01 000001
line04 detail02 000002
line05 detail03 000003
line06 detail04 000004
line07 detail05 000005
line08 detail06 000006
line09 detail07 000007
line10 Header01 Ends
line11 Header02 Starts blah var
line12 detail01 000001
line13 detail02 000002
line14 detail03 000003
line15 detail04 000004
line16 detail05 000005
line19 Header03 Ends

Here's what I need to do:

1. traverse each line and modify some section of the line
(it could postion 10-20) according to some logic in a function.

2. For Header line, I need to know the last detail line for that header
of a section (it could postion 20-25). So for Header02 (line11) I would
need to grab the value '000005' (line16).

The result for file01 will have it's line modified accordingly.

The main question is how can I traverse down line by line, and
when I get to the Header, somehow get the value of the last detail line
for that header, and continue the next line after the header which
is the first detail line.

I know you can do this in C by creating a dummy pointer to point
to the header and then traverse to the last detail line and return the
value. And then destroy the pointer.

I just don't know how to do this is .NET.

Thanks in advance for your replies!
Have a look at the FileStream class and its members:

http://msdn2.microsoft.com/en-us/library/system.io.filestream.aspx

http://msdn2.microsoft.com/en-us/library/system.io.filestream_members.aspx
 
I assume that you have no control over the file structure? There are easier
ways if you do.

First I wouldn't attempt to position, read & write these things with the
filestream. I think you'll be at it for a long time and (unless it has
gotten especially clever lately) you aren't going to be able to insert
things, i.e. lengthen or shorten any of the "entries". You'd also have a
lot of lookahead, skip back sort of action going on making it prone to
error.

What you are describing is the data portion of a class. See if you can
describe a class with properties representing whatever is on line01 and a
collection of headers and a collection of details within headers. Then
create a Load() method (using a filestream but notice you're only walking
the file from beginning to end) that reads a line, decides what it is and
adds it to an object of that class you designed.

Now you have it all in an object and clearly you can add other methods which
will locate a specific line, edit that line, etc. When you're all done you
call your Save() method which writes back a new text file.

Does that work for you?

Tom
 
Back
Top