Click here to Skip to main content
14,668,908 members
Rate this:
Please Sign up or sign in to vote.
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:
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'.

Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^]
   
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..
Rate this:
Please Sign up or sign in to vote.

Solution 2

You can do it with this code:
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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

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

Stephen.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100