Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a CSV file I am trying to read in using the following

C#
char[] delimetersChars = { '\n', '\r' };


   if (File.Exists("C:\\MeterIDs.csv"))
            {
                StreamReader sr = new StreamReader("C:\\MeterIDs.csv");
               // MessageBox.Show("Found it!");
                Defaults = sr.ReadToEnd();
                sr.Close();
                string[] Values = Defaults.Split(delimetersChars);
                foreach (string v in Values)
                    rtbLoaded.Text = rtbLoaded.Text + v+ ' ';
            }
            else
                MessageBox.Show("Not Found MeterIDs.csv");

This method has worked in the past just reading the data straight, however this time there are preceeding 0's on every other piece of data which are being lost as rbtLoaded has all the data expected but reading 0001 appears as 1, my first thought was one of the delimeters is taking the 0's off as they are there in NotePad++, but not when opened with Excel. Does anybody have any idea why they are lost in Excel but present in NotePad++?
HELP!
Glenn
Posted
Comments
Sergey Alexandrovich Kryukov 7-Dec-11 12:10pm    
This is text file; how it this related to Excel?
--SA
glennPattonWork3 7-Dec-11 12:21pm    
I though I was going nuts as when I opened the file with NotePad++ they were there and in asking the question I clicked on the file it opened with Excel and they are gone?? Vis Studio and Excel may use the same file standard which trims the 0's

The zeros are not shown in Excel because the cell format defaults to a simple number. If you want them to show up then change the cell format for the relevant column. However, why they are not showing in your code above is less easy to determine; I would suggest getting your debugger on the case to see exactly what data is being fed into your program.
 
Share this answer
 
Comments
glennPattonWork3 7-Dec-11 12:32pm    
I seem the same issue leading 0s disappear!
Richard MacCutchan 7-Dec-11 12:39pm    
That's most unlikely; I think you may be looking at the wrong file. Reading text in C# you will get exactly what is in the file.
I solved it, there was a file corruption issue, is the streamreader the best method of reading in files like that or is there another way??
 
Share this answer
 
v2
Comments
Richard MacCutchan 7-Dec-11 12:47pm    
StreamReader is fine if you are just reading some text strings and processing them internally.

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