0

I have a csv file. When I try to read that file using filestream readtoend(), I get inverted commas and \r at many places that breaks my number of rows in each column. Is there a way to remove inverted commas and \r. I tried to replace

 FileStream obj = new FileStream();
string a = obj.ReadToEnd();
a.Replace("\"","");
a.Replace("\r\"",""); 

When I visualize a all \r and inverted commas are removed. But when I read the file again from beginning using ReadLine() they appear again?

Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58
Calyfs0
  • 111
  • 1
  • 11
  • why don't you read line by line and unescape commas and newlines in each? then you could just split on commas – GSPdibbler Apr 22 '15 at 11:14
  • 2
    you're replacing the characters _in_ your string, not _in_ your file. – stefankmitph Apr 22 '15 at 11:15
  • What you see are non-printable characters, and are added in the specific tool you are using to view the content of the string you read. The Replace() operation works in the in-memory copy of the content of your file, and as such does not modify its content. – Alex Mazzariol Apr 22 '15 at 11:18
  • Simply removing double quotes and newlines will only corrupt your data - any comma inside double quotes is meant to be treated as data, not as a separator. – C.Evenhuis Apr 22 '15 at 11:21
  • So, what excactly is your problem at hand Sagar? Do you need help in writing your changes back to your file or do you just want an explanation of what is happening? – Marco Apr 22 '15 at 11:21
  • sorry man but it seems to me you do not want to use ReadToEnd() here if you want to count your number of rows and do some things with them in code. Also, a.Replace will not modify your file! Aside from that, in your example, you are not even changing your a because you don't assign it again, you need to do: a= a.Replace("a","b"); so the fact that you "dont see the quotes after your replace" is just because the escape characters are not visible when displaying. Anyways, this question could have had some more research in its entirety – stvn Apr 22 '15 at 11:48
  • I need to read this csv file, then I have a dictionary from where I map the header of the files with dictionary, then I need to save them to the database. User can have file in any format i.e. with headers or without headers. If there are no headers then I am taking first line as a header and number of columns as number of headers. Then each row must have length equal to the length of headers, if it mismatches I return. Due to these inverted commas and \r, my length doesn't match. – Calyfs0 Apr 22 '15 at 12:02

3 Answers3

3

First of all, a String is immutable. You might think this is not important for your question, but actualy it's important whenever you are developing. If I look at your code snippet, I'm pretty sure you have no knowledge of immutable objects so I advice you to make sure you fully understand the concept.

More information regarding immutable objects can be found: http://en.wikipedia.org/wiki/Immutable_object

Basicly, it means one can never modify a string object. Strings will always point to a new object whenever we change the value.

That's why the Replace method returns a value, which's documentation can be found here: https://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.110%29.aspx and states clearly that it Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

In your example, you aren't using the return value of the Replace function. Could you show us that the string values are actuably being replaced from your a variable? Because I do not believe this is going to be the case. When you visualize a string, carriage returns (\r) are not visual and replaced by an actual carriage return. If you debug and take alook at the actual string value, you should still see the \n.

Take the following code snippet:

var someString = "Hello / world";
someString.Replace("/", "");
Console.Log(someString);

You might think that the console will show "Hello world". However, on this fiddle you can see that it still logs "Hello / World": https://dotnetfiddle.net/cp59i3

What you have to do to correctly use String.Replace can be seen in this fiddle: https://dotnetfiddle.net/XCGtOu

Basicly, you want to log the return value of the Replace function:

var a = "Some / Value";
var b = a.Replace("/", "");
Console.WriteLine(b);

Also, as mentioned by others in the comment section at ur post, you are not replacing the contents of the file, but the string variable in your memory.

If you want to save the new string, make sure to use the Write method of the FileStream (or any other way to write to a file), an explanation can be found here: How to Find And Replace Text In A File With C#

Apart from all what I have been saying throughout this answer, you should not replace both inverted comma's and carriage returns in a file in most cases, they are there for a reason. Unless you do have a specific reason.

Community
  • 1
  • 1
Frederik Prijck
  • 1,424
  • 10
  • 16
  • Thanks, I really wasn't aware of mutable and immutable objects. I haven't reached a solution, will reply as soon as I finish it :) – Calyfs0 Apr 22 '15 at 12:38
0

At last I succeeded. Thanks to everybody. Here is the code I did.

    FileStream obj = new FileStream();
        using(StreamReader csvr = new StreamReader(obj))
{
        string a = obj.ReadToEnd();
        a = a.Replace("\"","");
        a = a.Replace("\r\"","");
        obj.Dispose();
}

        using(StreamWriter Wr = new StreamWriter(TempPath))
        {
          Wr.Write(a);

    }

    using(StreamReader Sr = new StreamReader(Tempath))
    {
    Sr.ReadLine();
    }

I Created a temp path on the system. After this things were easy to enter into database.

Calyfs0
  • 111
  • 1
  • 11
  • Hi, abit late but I've made some modifications. First of all, you are not correclty Disposing the FileStream. => https://dotnetfiddle.net/69mW8r Next thing: If your file gets for example 4GB, you will need 4GB of free RAM as you're reading the file entirely in your memore (ReadToEnd). This can be solved by streaming : https://dotnetfiddle.net/UUn3Ex BUT I am not sure this works for your use-case. But in general, it's better to not use ReadToEnd, but it might be okay for your situation! :-) – Frederik Prijck Jun 20 '16 at 07:10
-3

Try something like this

           StreamReader  sReader = new StreamReader("filename");
            string a = sReader.ReadToEnd();
            a.Replace("\"", "");
            a.Replace("\r\"", "");
            StringReader reader = new StringReader(a);
            string inputLine = "";
            while ((inputLine = reader.ReadLine()) != null)
            {
            }
jdweng
  • 33,250
  • 2
  • 15
  • 20