Click here to Skip to main content
16,004,678 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hey all,

I'm totally new to C# and am trying to read a comma delimited file into a fileStream, replace the commas with spaces and output the fileStream into a new file. When I use this code however the output seems to be encoded. Any ideas what I'm missing? Can anyone suggest an easier way of doing this, as I say I'm a total newbie to the language:
C#
private void Read_File(object Sender, EventArgs e)
{
     FileStream fileStream = new FileStream("C:\\commas.txt", FileMode.Open);
     String data = "";
     String data2 = "";
     byte[] bytes = new byte[fileStream.Length];
     try
     {
          using (var sr = new StreamReader(fileStream, Encoding.Unicode)) 
         { 
             data = sr.ReadToEnd(); 
         }
         for (int i = 0; i < data.Length; i++)
         {
             if (data.ElementAt(i).Equals(","))
             {
                 data2 = data2 + " ";
             }
             else
             {
                 data2 = data2 + data.ElementAt(i);
             }
          }
          File.WriteAllText("C:\\nocommas.txt", data2); 
      }
      finally
      {
          fileStream.Close();
      }
  }


Any help would be greatly appreciated.

Stephen.
Posted
Updated 19-Oct-12 5:27am
v2
Comments
[no name] 19-Oct-12 11:29am    
Use string.Replace instead. What is that you mean that the output is "encoded"?
Zasky 19-Oct-12 11:40am    
You're reading the file as unicode, which is probably isn't. That's why the output looks 'encoded'.

Since you are working with Plain Text stuff, you could use a TextReader and TextWriter instead. That would be the simple way. You can also just use a simple yourCurrentLine.Replace(",", " ") call to perform the conversion.
If your file is really large, use the ReadLine approach, if the file is guaranteed to be of a reasonable length, then you can use the ReadToEnd approach.
MSDN Documentation for TextWriter[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 19-Oct-12 19:13pm    
Marcus,

This time you are mixing up something. StreamReader is already derived from TextReader, and same goes about the writers. Not the other way around. So using these classes is coeeect. This is somewhat counter-intuitive naming, but it caught you.
--SA
fjdiewornncalwe 19-Oct-12 19:17pm    
D'oh..
You can do it with this code:
C#
private void Read_File(object Sender, EventArgs e)
{
  string contentOfFileWithCommas = File.ReadAllText("C:\\commas.txt",Encoding.UTF8);
  string contentOfFileWithoutCommas = contentOfFileWithCommas.Replace(",", " ");
  File.WriteAllText("C:\\nocommas.txt",contentOfFileWithoutCommas,Encoding.UTF8);
}

Hope this helps.
 
Share this answer
 
Thanks very much Marcus and ProgramFOX. I knew I was overcomplicating this as it seems such a simple concept. Thanks again.

Stephen.
 
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