473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 8969
:) <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Knighterrant | last post by:
How to combine multiple xml files into a single one, or split an xml file into multiple files using xslt?
4
by: PZWU | last post by:
How can I build a big XML file by appending a few smaller xml files in C#? Below is what I've been trying: /* FileString.xml is the path and name string of the big xml file*/ XmlTextWriter...
1
by: JimmySlam | last post by:
I have lots of files and i need to download them using an easy way. I only cany think how to getting the files , file by file. is it a quiker way to to this like downloading a foler o compressing...
0
by: amy | last post by:
Hi, all: > i am a new end user of access, now I have many excel files need to > import to One table in access (combine all excel files into one table > in excel). In excel files, some columns will...
1
by: Sergio | last post by:
Hello everybody. Somebody has given me nine files with some data for the years 1997, 1998, ... , 2005. They are 9 different mdb files. Each one has many tables, forms, and other things. But all...
4
by: kumar_ps | last post by:
I am using vb6.0 here i am using "tif" files my program is i am keeping some tif files in a folder. when i run the program that all the tif files combine one file. i mean suppose we take 4tif files...
5
by: Rodjk #613 | last post by:
Hello, I am working with a database that is in use in several locations. The structure of the databases are identical, but the data is different. I am tasked with combining the data into one...
7
by: SNN | last post by:
Hi, I have 4 files that I would like to combine side by side and the seperater is tab in these four files. I also would like to have the output file to be tab delimited. All 4 files have the same...
3
by: johnqsmith | last post by:
Problem background: gcc v 4.1 2 .cpp files, 2 .h files Files: main.cpp a.cpp a.h b.h
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.