Click here to Skip to main content
14,735,210 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have the following code, and a lot of text files. I want all files to be combined in 1, simply by adding them together. I.E file 1 is like

absdc
eiojs
sjkdi

file 2 is like
09asd
akjsd
341u0

the output will be:

absdc
eiojs
sjkdi

09asd
akjsd
341u0


I have the following function where I try to do this, but my string is not long enough :( How should I do this a better way?

string fullFile;
private void Merge()
{
    System.IO.StreamWriter data = new System.IO.StreamWriter(@"c:\" + Somevariable + @"\" + Somevariable + ".txt");
    for (int i = 1; i < 152; i++)
    {
        try
        {
            string[] fullfile = System.IO.File.ReadAllLines(@"c:\" + Somevariable+ @"\h" + i + ".7.txt");
            int length = fullfile.Length;
            for (int j = 0; j <= length; j++)
            {
                fullFile = fullFile+fullfile[j] + "\n"; // this string
            }
        }
        catch { i = 153; }
    }
    data.WriteLine(fullFile);
    data.Close();
}
Posted
Comments
Sergey Alexandrovich Kryukov 10-Oct-12 15:56pm
   
(Sigh...)

You catch { i = 153; } is such an abuse! I'm afraid I'll have trouble explaining you how to write code properly... :-(
The problem is very simple, but... almost every line show wrong approach in several aspects at the same time...
--SA
pieterjann 10-Oct-12 16:17pm
   
Wow, do I win a prize for writing such bad code? Iknow this is not the best approach but I wanted to try everything myself first before asking :)
Sergey Alexandrovich Kryukov 10-Oct-12 16:45pm
   
Almost, but it's hard to become and absolute winner :-)

You just start from here:
void MergeFiles(string outputFileName, params string[] inputFileNames) {/* ... */}

That is, don't start with code, start with reasonable general formulation of the problem: unlimited number of input files, unknown size of each; additionally, perhaps, don't assume they all exist. Write empty method body, main loop -- by input file names. Add detail step by step. No hard-coded immediate constants any more, all your "@C:\", 153, 152, etc. Also, there are absolutely no cases when hard-coded path names cab be useful. They are always calculated during run time bases on some data: user input, configuration, executable directory, "special folders", etc. Nothing is hard-coded. Also, best exception handling is no catching -- just let go. This is what exceptions are designed for -- to isolate them from main code.

And this is really, really easy.

I suggest you just start over, more think than click on keyboard keys, and you will be fine.
--SA
pieterjann 10-Oct-12 17:51pm
   
thank you, I will rewrite my code soon and take this in concideration :) Only 4 functions written, and it works and i have no clue anymore how... I should rewrite it before I can add more functionality :)
Sergey Alexandrovich Kryukov 10-Oct-12 18:45pm
   
Good idea. Good luck,
--SA
[no name] 10-Oct-12 16:28pm
   
Since you are simply appending one, or more files, to the first file, why not look into using the File.AppendText method?
Sergey Alexandrovich Kryukov 10-Oct-12 16:46pm
   
Right idea, but -- OP's problem is different. Please see my big comment above. (You infected me with this manner of writing comments, not solutions.)
--SA
[no name] 10-Oct-12 16:51pm
   
You should do solutions. My comments are just comments. Too lazy to do solutions most of the time :-) Would rather the OP do the work....
Sergey Alexandrovich Kryukov 10-Oct-12 17:20pm
   
I don't quite know myself what "I should", how can you know that? :-)
Thank you very much for your comment though.
Would rather the OP do the work is right thing. Useful for OP, first of all.
--SA

I would recommend trying the the second one in this list:

http://stackoverflow.com/questions/6311358/efficient-way-to-combine-multiple-text-files[^]
http://www.c-sharpcorner.com/uploadfile/sonuraj/merge-two-files-with-C-Sharp/[^]
http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/0faebd8f-339b-4f74-96e0-d4a71634ade0[^]

Your code assumes that the files are text files, which may be OK, but is not necessarily true. Of course could have problems with using bytes in that you have text files, but they are different encodings.
   
v2
You can achieve this using strings also. If you are using file then just concatenate both to join.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900