473,394 Members | 2,063 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,394 software developers and data experts.

Split a text file into several based on output file size.

TJ
I am very new to C# (this is my first real project), therefore please be
patient if my question is considered being to newbie.

I am modifying a program which takes a text file, does some formatting then
outputs the resulting file. What I am trying to do is have the program make
as many files as is needed, each limited to a given file size (in this case
240KB).

As way of example say I have a text file which is 723KB (I get this by using
Fileinfo.length) named Test.txt. Program runs and does it's formatting and
is ready to save the output file. In this case the program should make the
following files:

Test1.txt (240KB)

Test2.txt (240KB)

Test3.txt (204KB)

Test4.txt (3KB)

Can anyone offer either in simple steps a solution or place to start to
perform this operation. Remember I am not trying to determine the size of
an existing file, but trying to output X number of files based on a given
maximum file size for each.

Thank you,

tj
Nov 16 '05 #1
1 10733
To clarify what you're trying to do:
- you want your application to take in the name of a text file
- another parameter you want is the max size of each "chunk"
- then you want your application to return how many "chunks" the text file
will be split into?
- presumably you want your application to perform this split?

The easiest (perhaps not the most efficient) way to do this would be to open
the file and read out the contents into a StreamReader.

int maxChunkSizeKB = 240; // size in kilobytes
int maxChunkSizeB = maxChunkSizeKB * 1024; // convert to bytes (==
characters)
string textFileName = @"C:\test.text";
StreamReader rdr = File.OpenText ( textFileName );
then take this file and place it into a string buffer

string fileBuffer = rdr.ReadToEnd();
rdr.Close();
now cut up the file into sizeable files

int fileCounter = 0;
while ( fileBuffer.Length > 0 )
{
//
// get this chunk contents

fileCounter++;
string fileChunkBuffer = "";
if ( fileBuffer.Length > maxChunkSizeB )
{
fileChunkBuffer = fileBuffer.SubString( 0, maxChunkSizeB );
fileBuffer = fileBuffer.SubString ( maxChunkSizeB );
}
else
{
fileChunkBuffer = fileBuffer;
fileBuffer = "";
}

//
// write this chunk file

string chunkFileName = textFileName + fileCounter.ToString();
StreamWriter wtr = File.CreateText ( chunkFileName );
wtr.Write( fileChunkBuffer );
wtr.Close();
}
The number of files that were created is therefore equal to the value of
fileCounter.
I didn't compile this so you may need to tweak it.

Hope that helps.

Daniel.

"TJ" <TJ@nospam.me> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
I am very new to C# (this is my first real project), therefore please be
patient if my question is considered being to newbie.

I am modifying a program which takes a text file, does some formatting
then outputs the resulting file. What I am trying to do is have the
program make as many files as is needed, each limited to a given file size
(in this case 240KB).

As way of example say I have a text file which is 723KB (I get this by
using Fileinfo.length) named Test.txt. Program runs and does it's
formatting and is ready to save the output file. In this case the program
should make the following files:

Test1.txt (240KB)

Test2.txt (240KB)

Test3.txt (204KB)

Test4.txt (3KB)

Can anyone offer either in simple steps a solution or place to start to
perform this operation. Remember I am not trying to determine the size of
an existing file, but trying to output X number of files based on a given
maximum file size for each.

Thank you,

tj

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: ross | last post by:
I want to do some tricky text file manipulation on many files, but have only a little programming knowledge. What are the ideal languages for the following examples? 1. Starting from a certain...
2
by: Jen | last post by:
Trying to take one table in access and split it into multiple excel files(using an excel template); and then email based on email addresses in Table2; Of course, I would like to do all of this...
3
by: Krish | last post by:
I have requirement, that i get one big chunk of text file. This text file will have has information, that on finding "****End of Information****", i have to split them individual text file with our...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: Sen Haerens | last post by:
I'm using string.split(/^$/m, 2) on a curl output to separate header and body. There’s an empty line between them. ^$ doesn’t seem to work... Example curl output: HTTP/1.1 404 Not Found...
6
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large...
2
by: hash4sp | last post by:
I have just started my career in programming. My Problem is reading a text file using VB.NET. The text contains the state names followed by respective city names. My task is to read the file and...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
7
by: John Smith | last post by:
Hi, I am very new to C# and NET framework. I am trying to hash (using MD5CryptoServiceProvider) a source that is split into several files. Now when the source is in one file I can produce the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.