471,319 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

combine serveral .txt files

:)
How to combine serveral large .txt files in C# without using MS command
line. Thanks!!

Jan 16 '06 #1
6 8883
:) <ta******@hotmail.com> wrote:
How to combine serveral large .txt files in C# without using MS command
line. Thanks!!


Unless you really care about them being text files (and changing the
encoding, for example) I'd just copy them as binary files:

1) Open a stream to write to
2) Open the first stream to read from
3) Read chunks at a time, writing to the output stream each time
4) When that file has been finished, close the stream and move onto
the next file (back to step 2)
5) When you've finished all the files, close the output stream.

Note that you should always use the return value from Stream.Read to
find out how many bytes have actually been read.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 16 '06 #2
Read each text file into a string. Then concatenate the strings.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

":)" <ta******@hotmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
How to combine serveral large .txt files in C# without using MS command
line. Thanks!!

Jan 16 '06 #3
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
Read each text file into a string. Then concatenate the strings.


That's potentially very, very expensive. It's not entirely unreasonable
to want to concatenate files which are larger than physical memory...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 17 '06 #4
> That's potentially very, very expensive. It's not entirely unreasonable
to want to concatenate files which are larger than physical memory...
Yes it is, Jon. But he didn't asy what he wanted to do with the "combined"
files. Since they were text files, they are equivalent to strings. There are
2 possible things to do with such files: (1) Combine them as a file, or (2)
Combine them as a string. I gave him the solution to number 2. Note that I
did not continue with how to write the string to a file. If I were giving
him the solution to number 1, I would not have talked about using strings at
all.

One might assume that he was talking about combining them as a file, but one
of the reasons I have trouble communicating with people is that I have
trained myself not to make assumptions. It makes me a better developer.

Under the circumstances, I could have asked for more specific information,
but being of British descent (I'm sure you understand Jon), I decided to
answer one of the 2 possibilities with a subtle subtext regarding the nature
of the question. Another reason I have trouble communicating with people is
that my subtext is often so subtle that it is lost on the audience. However,
I have found that a subtle subtext is often less likely to incur anger
towards the messenger, on the part of the listener. As the subtext is
somewhat ambiguous, it is either missed, in which case I dodge the
proverbial bullet, or it is received, in which case the communication is
made, but the ambiguity provides enough plausible deniability as to divert
any possible resentment.

Bottom line, when a person asks a question, he/she should be specific about
the requirements. GIGO.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
Read each text file into a string. Then concatenate the strings.


That's potentially very, very expensive. It's not entirely unreasonable
to want to concatenate files which are larger than physical memory...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 17 '06 #5
:)
Thanks for your answer. My requirement is combine servral .txt file
into one .txt files.

Jan 17 '06 #6
Sure. In that case, Jon's answer would be what you need to do. To quote Jon:

Unless you really care about them being text files (and changing the
encoding, for example) I'd just copy them as binary files:

1) Open a stream to write to
2) Open the first stream to read from
3) Read chunks at a time, writing to the output stream each time
4) When that file has been finished, close the stream and move onto
the next file (back to step 2)
5) When you've finished all the files, close the output stream.

Note that you should always use the return value from Stream.Read to
find out how many bytes have actually been read.

One possible caveat here: This technique will not work as is unless all of
the files are using the same encoding. Chances are, they are, but you should
make sure if they are not. If they are not the same encoding, you can
convert them to a common encoding on the fly if necessary.

If you're not sure all of the files have the same encoding, the following
article tells you how to detect the encoding of a file:

http://www.dotnet247.com/247referenc...utorial_id=469

Once you've detected the encoding, you will want to read the file into an
array of bytes, and convert it using the Encoding.Convert method. You can
then write the bytes to a File Stream. Keeping the File Stream open, you can
open, read, convert, and write each successive file to the new text file in
the same way until you're done.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
":)" <ta******@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Thanks for your answer. My requirement is combine servral .txt file
into one .txt files.

Jan 17 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Knighterrant | last post: by
4 posts views Thread by PZWU | last post: by
5 posts views Thread by Rodjk #613 | last post: by
7 posts views Thread by SNN | last post: by
reply views Thread by rosydwin | last post: by

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.