Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi ,

Please provide me advise on below task.

C# Program to Execute given text file into output text file

Input.txt

Col1 Col2
Id1 Val1
Id2 Val2
Id3 Val3
1d1 Val4
Id4 Val5
Id2 Val6
Idn

ouput.txt

Col1 Col2
Id1 Val1;val4
Id2 Val2;val6
Id3 Val3
Id4 Val5
Posted
Comments
CHill60 28-Nov-14 5:46am    
We don't do your homework for you. How would you learn if we did? Give a try, it's not hard, then come back if you have a specific problem

Hi,

Try this:

C#
// For store input data;
var data = new Dictionary<string, List<string>>();

// Load data from file
using (var streamReader = new StreamReader(@"D:\input.txt"))
{
    while(!streamReader.EndOfStream)
    {
        var line = streamReader.ReadLine();
        var items = line.Split(" ".ToCharArray());
        var id = items[0];
        var value = items[1];
        if (!data.ContainsKey(id))
        {
            data.Add(id, new List<string>());
        }
        data[id].Add(value);
    }
}

// Sava file
using (var streamWriter = new StreamWriter(@"D:\output.txt"))
{
    foreach (var key in data.Keys)
    {
        var values = string.Join(";", data[key]);
        streamWriter.WriteLine(string.Format("{0} {1}", key, values));
    }
}


Some links for education:
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamreader%28v=vs.90%29[^]
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter%28v=vs.90%29[^]
http://msdn.microsoft.com/en-us/library/vstudio/xfhwa508%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.collections%28v=vs.110%29.aspx[^]

Cheers!
 
Share this answer
 
v2
Comments
Richard MacCutchan 28-Nov-14 5:57am    
You don't help people by doing their homework for them.
Marcin Kozub 28-Nov-14 9:36am    
I like to help people and user didn't ask just for code. He/she asked for advice, that's a big difference. Another thing is that sometimes 'One image is worth more than thousands of words'. That rule can be applied to coding too. IMO quick look at the source code can be much more helpful that reading tons of documentation.
I don't do someones homework. I see many users here on CP and other QA sites asking for code. But if you take a look at user's profile you will see that he/she had contribution in CP by answering other's user question.
Maciej Los 28-Nov-14 7:58am    
+5!
Marcin Kozub 28-Nov-14 9:19am    
Thx Maciej ;)
Hi Please try this
C#
string[] lines = System.IO.File.ReadAllLines(@"E:\Test\Input.txt");

            var r = from x in lines.Select(x => x.Split('\t')).OrderBy(x=>x[0])
                    group x by x[0] into p
                    select p;
            string Value = string.Empty;
            string Key = string.Empty;
            
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:\Test\Output.txt"))
            {
            foreach (var n in r)
            {
                Console.WriteLine(n.Key);
                Key = n.Key;
                foreach (var l in n)
                {
                    Value = Value + l[1] + ";";
                }
                file.WriteLine(Key + " " + Value);

                Value = "";
            }
            }
 
Share this answer
 
v2
Comments
Maciej Los 28-Nov-14 7:58am    
In C# string is inmutable! I strongly recommend to use StringBuilder instead of string concatenation:
Value = Value + l[1] + ";";.
The rest is worth 4!
Hi ,

I found one solution . Please find the below code.

C#
string[] lines = System.IO.File.ReadAllLines(@"input.txt");
            string fnString = "", keyStr = "";
            ArrayList arLst = new ArrayList();
            arLst.Add("Col1 Col2");
            for (int i = 1; i < lines.Count(); i++)
            {
                fnString = ""; keyStr = "";
                if (lines[i].Trim() != "")
                {
                    string[] arrRow = lines[i].Split(' ');
                    for (int j = 1; j < lines.Count(); j++)
                    {
                        if (lines[j].Trim() != "")
                        {
                            string[] arrRow1 = lines[j].Split(' ');
                            keyStr = arrRow[0];
                            if (arrRow[0].Equals(arrRow1[0]))
                            {
                                fnString += arrRow1[1] + ";";
                                lines[j] = "";
                            }
                        }
                    }
                    if (fnString.EndsWith(";"))
                        fnString = fnString.Substring(0, fnString.Length - 1);
                    arLst.Add(keyStr + ' ' + fnString);
                }
            }
            using (var streamWriter = new StreamWriter(@"output.txt"))
            {
                for (int i = 0; i < arLst.Count; i++)
                {
                    streamWriter.WriteLine(arLst[i]);
                }
                streamWriter.Close();
            }
 
Share this answer
 

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