Click here to Skip to main content
14,391,825 members
Rate this:
Please Sign up or sign in to vote.
See more:
I wanted to read text file in C#. Code works well but I notcied that my code was not able to read the decimals. For example, if I have 159.58, I got in GridView 15958 even I defined the columns in DataGridView as double. Any Idea?

What I have tried:

<pre>public void ImportData()
        {

<pre>
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Import text file";
        ofd.InitialDirectory = @"C:\";
        ofd.Multiselect = true;
        ofd.Filter = "txt file (*.txt)|*.txt| all files (*.*)|*.*";

        try {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                label1.Text = ofd.FileName;

            }

            string[] lines = File.ReadAllLines(ofd.FileName);
            string[] value;
            int count = 0;
            for (int i = 0; i < lines.Length; i++)
            {
                value = lines[i].ToString().Split(',');
                string[] rows = new string[value.Length];

                for (int j = 0; j < value.Length; j++)
                {
                    rows[j] = value[j].Trim();
                    count ++;

                }
                dt.Rows.Add(rows);
            }
            MessageBox.Show("The file has been read from" + label1.Text+". It has"+" "+count/3+"lines");
        }
        catch( Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

     }
Posted
Updated 5-Nov-19 5:06am
v3
Comments
Maciej Los 30-Oct-19 5:07am
   
And your text file structure is...
MadMyche 30-Oct-19 7:17am
   
Post a snippet of the text file itself, so that we can what type of "text" it really is
EngAb1989 30-Oct-19 7:21am
   
Have you any idea how can I put snapshot as attachments here in code project website?
EngAb1989 30-Oct-19 7:22am
   
How can I attach?
Rate this:
Please Sign up or sign in to vote.

Solution 2

It could be a cultural thing, see: c# 4.0 - how to set default culture info for entire c# application - Stack Overflow[^]
As you probably are using a German Windows version, comma's are expected for the decimals.

It could also be that you have comma's for the decimals in your text file, the Split(',') will then truncate them.
It would be better to use ';' as a separator (or TAB if you prefer that) and then use Split(';').
   
v3
Comments
EngAb1989 5-Nov-19 10:14am
   
I change the current thread to English because you are right, I have Deutsch thread. But the problem is still. maybe can look at my code
Rate this:
Please Sign up or sign in to vote.

Solution 1

It's going to depend on other code: specifically the code you use to read the actual text file into your app - and that will depend on how the data in your text file is organised.

"Text files" have no "fixed format" - you can stick anything you like in there, it doesn't even have to contain just human readable data (or even human readable data!) For example, CSV data is text, XML data is text, JSON data is text. So is "one line per item", Base64, and INI - but you can't just "read the text file" and have teh actual data elements understandable in your app.

So start by looking at the input file using a text editor - notepad will probably do - and identify what kind of data it contains.
Then look at the code which reads that file, and check exactly what it does with it.

If it all looks reasonable, then use the debugger to look at exactly what it reads in, and exactly what it does with it.


Sorry, but we can't do any of that for you - we have no access to your app, or the data you are feeding it from!
   
Comments
EngAb1989 5-Nov-19 10:02am
   
Hallo...
I have text file (*.txt) the data is shown like below:
1DSC03234.JPG,22.77615315,113.3233106,152.2
1DSC03235.JPG,22.77629059,113.3233118,152.2
1DSC03236.JPG,22.77642722,113.3233118,151.8
1DSC03237.JPG,22.7765603,113.3233117,152.2
1DSC03238.JPG,22.77659317,113.323343,151.9
1DSC03239.JPG,22.77659454,113.3234567,151.8
1DSC03240.JPG,22.77658938,113.3235803,151.2
1DSC03241.JPG,22.77655948,113.3236162,151.5
1DSC03242.JPG,22.77644701,113.3236192,150.6
1DSC03243.JPG,22.77632312,113.323616,150.9
1DSC03244.JPG,22.77619341,113.3236129,151.5
1DSC03245.JPG,22.77606491,113.3236104,151.6
1DSC03246.JPG,22.77593788,113.3236093,151.5
1DSC03247.JPG,22.77580687,113.323607,153.2
1DSC03248.JPG,22.77567569,113.323605,153.7
1DSC03249.JPG,22.77553981,113.3236008,153.7
1DSC03250.JPG,22.77539983,113.3235962,153.7
1DSC03251.JPG,22.77526019,113.3235955,152.6


The part of my code for reading the text file is attached
OriginalGriff 5-Nov-19 10:07am
   
And? What does the debugger show you?
0x01AA 5-Nov-19 13:57pm
   
It looks your decimals do use a fix decimal separator (not culture dependend). So use double.Parse("3.5", CultureInfo.InvariantCulture) and more better the double.TryParse

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