File read and write new

  • Thread starter Thread starter Martin Stöferle
  • Start date Start date
M

Martin Stöferle

Hello,

how is the best way to read this file.
I need only the section :C-DATENIMPORT

I make something and write the whole file new.

So in any case I shold read all, memorize the rows and write to a new file.

With best regards Martin

Original.txt
:A-KUNDE-2701
261.80,002.80,000.00
266.80,002.80,000.00
271.80,002.80,000.00

:B-KUNDE-1203
dddd,eeeee,ddddd
281.80,002.80,000.00,NDC

:C-DATENIMPORT
;Liste
126.13,66.25
266.34,76.25
406.56,66.25
126.13,127.84
266.34,127.84
406.56,127.84
11.45,140.27
151.59,140.27
291.73,140.27
11.45,201.86
151.59,201.86
291.73,201.86
11.45,263.46
151.59,263.46
291.73,263.46

:D-KUNDE-55555
;Liste
MarkInk01,MarkInk02,MarkInk03,MarkInk04


~~~~~~~~~~~~~~~~~~




NEW.txt
:A-KUNDE-2701
261.80,002.80,000.00
266.80,002.80,000.00
271.80,002.80,000.00

:B-KUNDE-1203
dddd,eeeee,ddddd
281.80,002.80,000.00,NDC

:C-DATENIMPORT
;Liste
11.45,140.27
11.45,201.86
11.45,263.46
126.13,66.25
126.13,127.84


:D-KUNDE-55555
;Liste
MarkInk01,MarkInk02,MarkInk03,MarkInk04
 
Good morning,
For reading text files, the StreamReader class provides good support.
You can pass it a filename to the constructor, and it will automatically
open the file for you. The class provides methods for reading strings,
handling character encoding conversions as necessary.


What does "write the whole file new" mean?
I should read the whole file, make modification in the structure :C
After that, it is necessary to write the file new.

Do you want to wind up with
literally an exact copy of "the whole file"? Or did you actually that
you want the same filename as the original file, but with only section
of interest (":C-DATENIMPORT")? yes.

If you are writing to a whole new file, there is no need to read all of
the original file at once. Just read one line at a time, keeping track
of whether you are at a point in the input file where the lines need to
be written to the new file or not. In fact, if you have only one
":C-DATENIMPORT" section, you only need to read the input file far
enough until you've reached the end of that section. How?

Even if you really just want the original file to have new contents,
with just that section, IMHO the best way to achieve that is to write to
a temp file (see Path.GetTempFileName()) and the once you've
successfully written the new file, delete the original and move the temp
file to the original filename (see File.Move()). The actual writing to
the temp file would be just as in my previous suggestion.
Martin
 
Hello,
I still don't understand. Will the new file contain only "the structure
:C"? Or will the new file contain all of the sections ("structures")
found in the original file, but with modifications to "the structure :C"?
yes of course, the new file use all structures
with the modification to "the structure :C"

In the new file the item list is changed. That is the reason for writing
a new file.

:C-DATENIMPORT
;Liste
11.45,140.27
11.45,201.86
11.45,263.46
126.13,66.25
126.13,127.84


Original.txt
:A-KUNDE-2701
261.80,002.80,000.00
266.80,002.80,000.00
271.80,002.80,000.00


:B-KUNDE-1203
dddd,eeeee,ddddd
281.80,002.80,000.00,NDC


:C-DATENIMPORT
;Liste
126.13,66.25
266.34,76.25
406.56,66.25
126.13,127.84
266.34,127.84
406.56,127.84
11.45,140.27
151.59,140.27
291.73,140.27
11.45,201.86
151.59,201.86
291.73,201.86
11.45,263.46
151.59,263.46
291.73,263.46


:D-KUNDE-55555
;Liste
MarkInk01,MarkInk02,MarkInk03,MarkInk04


~~~~~~~~~~~~~~~~~~


NEW.txt
:A-KUNDE-2701
261.80,002.80,000.00
266.80,002.80,000.00
271.80,002.80,000.00


:B-KUNDE-1203
dddd,eeeee,ddddd
281.80,002.80,000.00,NDC


:C-DATENIMPORT
;Liste
11.45,140.27
11.45,201.86
11.45,263.46
126.13,66.25
126.13,127.84


:D-KUNDE-55555
;Liste
MarkInk01,MarkInk02,MarkInk03,MarkInk04
 
Hello Pete,
Then my previous suggestion, with respect to reading the input file one
line at a time until you reach the ":C" section, still applies.
However, upon reaching that section, you would then write to the output
file your new section data instead of the lines found in the input file.

Once you've written those new lines out, you would then continue to read
the input file, ignoring the lines of data until you reach the end of
the ":C" section of the original input file. Finally, once you reach
the end of the ":C" section of the original input file, you would resume
simply copying the lines from the input to the output file.

Mark's reply provides some insight as to the basic logic of dealing that
kind of operation -- that is, using a flag to keep track of the state of
the input file. However, rather than using the flag as he suggested
(his suggestion being based on a very reasonable, but apparently
incorrect, guess of what your original question meant), the flag's use
would be reversed. That is, it would be set to "true" _until_ you reach
the ":C" section, at which point you would set it to "false". After
writing out the new data, you would resume reading the input file,
setting the flag back to "true" once you reach the end of the ":C"
section in the input file (e.g. find a blank line).

In this way, the flag controls whether you are copying lines from the
original file to the new file, and a separate method can write the new
lines of the ":C" section at the appropriate time (basically, when you
are setting that flag to "false").
it is ok, thanks, the logic way is now clear.

Have a nice evening.

Regards Martin
 
Back
Top