I have a CSV file I read in and I convert it to a TXT file by writing out the values comma seperated per column. I want the program to also be able to convert the TXT file back to a CSV so I'm creating a TXTReader
class. I'm having trouble reading in big TXT files. I first tried it using String.Split
:
string fullText = File.ReadAllText(fileName);
string[] values = fullText.Split(',');
This worked at first but started causing problems when columns with strings that had commas in them showed up, making the program think it was another column while it was just a string. I went on to find a solution and found https://stackoverflow.com/a/3147901/1870760. This works perfectly with small files but is really slow with my 31 MB TXT files. I then tried my own hacky way by iterating over all the characters in fullText
and checking for "\""
because all of the strings have quotes wrapped around them in the TXT but this also took a long time (~10 minutes).
I also can't use https://stackoverflow.com/a/3148691/1870760 because my string column values sometimes contain \n
which causes the reader to think it's a new row, which it's not.
So, do I have to just accept it'll take a while to read a 31 MB TXT file and splitting the values into columns or are there more performance effective ways to do this?