Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Looking to do something which should be straight forward. I want to take a file and read in each line and add a tab after the first word of each line and write it out to a new file. here is my code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace addTab
{
    class Program
    {
        static void Main(string[] args)
        {

            DirectoryInfo dir = new DirectoryInfo(@"C:\work1");
            int n=0;
            foreach (System.IO.FileInfo fi in dir.GetFiles())
            {
                string[] lines = System.IO.File.ReadAllLines(@"C:\work1\" + fi);
                foreach (string line in lines)
                {
                        string[] words = line.Split(new Char[] { ' ' });
                        words[0] = words[0] + '\t';
                        using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
                        {
                            writer.WriteLine(words);
                        }
                    n++;
                }
            }
            string qq = Console.ReadLine();
        }
    }
}


Instead of writing out each line with the original line plus the tab after the first word, it is writing out
System.String[]


Any Suggestions?
Posted
Comments
Sergey Alexandrovich Kryukov 24-Oct-12 13:33pm    
What's the problem?
(Bad code, for sure: hard-coded immediate constants (there are no cases when hard-coded path names could be useful -- never at all, except some trial code), string concatenation is not effective, the generation of such text looks pointless, there will be problems if some files are really big, but the problem would be solvable if you did not real all lines at once, "n" is not used,...)
--SA

1 solution

The String[] type's .ToString() method does not automatically concatentate all of the elements of the string. In fact, this is just the default Object.ToString() method which just returns the name of the type.

Try this:

C#
foreach (FileInfo fi in dir.GetFiles())
{
  // Don't re-open the log file for every line; once per file is plenty.
  // Could even move this out to wrap the foreach above
  using (StreamWriter writer = new StreamWriter(@"C:\log.txt", true))
  {
    string[] lines = File.ReadAllLines(fi.FullName);
    foreach (string line in lines)
    {
      string[] words = line.Split(new Char[] { ' ' }, 2);  // Only split at the first space
      writer.WriteLine(words[0] + '\t' + words[1]);  // add the tab, reassemble and write it
      //n++;     // unused?
    }
  }
}
 
Share this answer
 
v2
Comments
fjdiewornncalwe 24-Oct-12 13:52pm    
+5. Even though I suspect this is homework. It's just a great answer. Cheers :)
stevenandler 25-Oct-12 16:36pm    
Thank you Matt. Actually I needed to do this for a customer so I could import the data nicely into Excel. I suspect there may have been other ways do do this but this works fine.

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