11 Replies - 564 Views - Last Post: 13 September 2010 - 11:57 AM Rate Topic: -----

#1 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

C# Read binary values from file output the result

Posted 13 September 2010 - 05:48 AM

I have a .txt file with the following data in it, Im trying to read the file, and output the exact result in the console or a messagebox. My code does not output anything when I open the file nothing is shown ? Am I missing an array or something

.txt content =
22.08788214
98.58476277
90.30419021
66.95541915
71.69797604
88.34645813
24.89229343
79.37111899
75.8637856
68.93948998
41.47722297
16.78244013
98.42130636
29.44479061
36.44316384
80.20724065
75.6296787




public partial class Form1 : Form
    {
           
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                double d = br.ReadDouble();
                String str = br.ReadString();
                br.Close();
                fs.Close();

                Console.WriteLine(d);
            }

        }
    }
}
    

This post has been edited by greenwood: 13 September 2010 - 05:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C# Read binary values from file output the result

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,526
  • Joined: 23-August 08

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 06:03 AM

Why are you using a BinaryReader if it's a text file?
Was This Post Helpful? 0
  • +
  • -

#3 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 06:08 AM

View PostJackOfAllTrades, on 13 September 2010 - 05:03 AM, said:

Why are you using a BinaryReader if it's a text file?



Because the values in my text file are not text but numbers ? Im new to this so correct me if Im wrong.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,526
  • Joined: 23-August 08

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 06:11 AM

It's still TEXT though. You read it as text, then convert to the double format you need.
using System;
using System.IO;

namespace blah
{
    public class Test
    {
        public static void Main()
        {
            using (var fs = File.OpenRead("testfile.txt"))
            {
                using (var sr = new StreamReader(fs))
                {
                    string s;
                    while ((s = sr.ReadLine()) != null)
                    {
                        double data = 0.0;
                        if ((Double.TryParse(s, out data)))
                            Console.WriteLine(data);
                    }
                }
            }
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#5 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 06:25 AM

View PostJackOfAllTrades, on 13 September 2010 - 05:11 AM, said:

It's still TEXT though. You read it as text, then convert to the double format you need.
using System;
using System.IO;

namespace blah
{
    public class Test
    {
        public static void Main()
        {
            using (var fs = File.OpenRead("testfile.txt"))
            {
                using (var sr = new StreamReader(fs))
                {
                    string s;
                    while ((s = sr.ReadLine()) != null)
                    {
                        double data = 0.0;
                        if ((Double.TryParse(s, out data)))
                            Console.WriteLine(data);
                    }
                }
            }
        }
    }
}




I have a menu strip with the options to choose the file to open, if I do this, still I get no output in console from my file.
namespace test
{
    public partial class Form1 : Form
    {
           
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                String nbr;

                while ((nbr = sr.ReadLine()) != null)
                {
                    double data = 0.0;  
                    if ((Double.TryParse(nbr, out data)))  

                           Console.WriteLine(data);  

                }
                sr.Close();
                fs.Close();
                
            }
        }

        }
 }


Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,526
  • Joined: 23-August 08

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 06:29 AM

Output to WHERE??? That sample code is writing to the console, which in YOUR app is probably not going anywhere at all. I didn't write this to your spec, I wrote it as an example. You need to put the data where you want it.
Was This Post Helpful? 1
  • +
  • -

#7 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 08:12 AM

View PostJackOfAllTrades, on 13 September 2010 - 05:29 AM, said:

Output to WHERE??? That sample code is writing to the console, which in YOUR app is probably not going anywhere at all. I didn't write this to your spec, I wrote it as an example. You need to put the data where you want it.



Thanks this worked fine :-) But one minor problem, how do I specicfy whn it reaches the end of the text file, so it can stop reading.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3801
  • View blog
  • Posts: 6,408
  • Joined: 08-June 10

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 08:34 AM

This line does that:
while ((s = sr.ReadLine()) != null)


Alternatively, if it is a relatively small file, you could read the whole file with one call using sr.ReadToEnd() and then parse it.
Was This Post Helpful? 0
  • +
  • -

#9 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 09:19 AM

Ok I did not notice the != null, still quite new heh! Thanks
But if I wanted to put the content(data) in a variable after its been read,
would I put it in a array ? If so to what I should initialize the array ?
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3801
  • View blog
  • Posts: 6,408
  • Joined: 08-June 10

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 09:27 AM

I would suggest a List<double>. Instead of Console.WriteLine(data), you would use list.Add(data).
Was This Post Helpful? 1
  • +
  • -

#11 greenwood  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 24
  • Joined: 09-September 10

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 11:51 AM

View PostinsertAlias, on 13 September 2010 - 08:27 AM, said:

I would suggest a List<double>. Instead of Console.WriteLine(data), you would use list.Add(data).



Thanks for the suggestion, but I never used List<double>, is that the only change I should do replace Console.WriteLine(data) for list.Add(data)? Should I initialize List<double> somewhere like in
 private void openToolStripMenuItem_click(oject sender, EventArgs e, List<double> data) 
?
Please advise :-)

This post has been edited by greenwood: 13 September 2010 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,526
  • Joined: 23-August 08

Re: C# Read binary values from file output the result

Posted 13 September 2010 - 11:57 AM

The Manual

Quick and dirty:

Create a class member variable
List<double> dataValues = new List<double>();


Substitute dataValues.Add(data); for Console.WriteLine().

Use the List later as you see fit. Refer to the documentation provided above.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1