5 Replies - 1254 Views - Last Post: 05 July 2011 - 04:01 AM Rate Topic: -----

#1 phpa80   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-July 11

create an array of numbers from numbers stored in textfile.

Posted 04 July 2011 - 11:56 AM

hi there.i want to implement C# Program in which get text file , in that text file an array of numbers stored . like this :
2,8,3
1,4,6
7,2,3
6,5,7
1,5,6


i want to calculate specific mathematical operation on each of rows in that text file and then results printout in another text file(line by line).by now i could write some code so that i can load file and create an array of number and split characters but i don't have any idea what can i do in the next step. can anyone help me ?

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "Text Files (*.txt)|*.txt";
            DialogResult dr=op.ShowDialog();
            if (dr==DialogResult.OK)
            {
               
                string [] str = File.ReadAllLines(op.FileName);
                foreach (string st1 in str)
                {
                    string[] spl = st1.Split(',');
                   
                }
            }


This post has been edited by phpa80: 04 July 2011 - 11:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: create an array of numbers from numbers stored in textfile.

#2 fixo   User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: create an array of numbers from numbers stored in textfile.

Posted 04 July 2011 - 01:15 PM

See if this will work for you
                string[] str = File.ReadAllLines(op.FileName);

                foreach (string st1 in str)
                {

                    string[] spl = st1.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
          
                        int a, b, c;
                     if (!Int32.TryParse(spl[0], out a)) return;
                     if (!Int32.TryParse(spl[1], out B)/>) return;
                     if (!Int32.TryParse(spl[2], out c)) return;
                     int result = a + b + c;//<--your formula here
                    MessageBox.Show(string.Format("A = {0}, B = {1}, C = {2}\nA + B + C = {3}",a,b,c,result));

                }


Was This Post Helpful? 1
  • +
  • -

#3 itpro4470   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: create an array of numbers from numbers stored in textfile.

Posted 04 July 2011 - 08:14 PM

Here's some sudo code for ya..
It's also a good idea to break up the different parts in the sudo code into separate functions. Try to have one function do one thing for example one function to read the file contents and one function to perform the calculations.. You don't have to do this but it will improve the readability of your code.

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "Text Files (*.txt)|*.txt";
            DialogResult dr=op.ShowDialog();
            if (dr==DialogResult.OK)
            {
                //Make sure the file is in fact a text file
                //Read all of the lines in the file
                //Iterate through each line
                    //Convert values to integers
                    //Make sure to catch for conversion errors for example 
                    //if a user puts a letter there needs to be a messagebox
                    //to scold them
                    //Perform calculations
                //Present the data back to the user
            }


This post has been edited by itpro4470: 04 July 2011 - 08:16 PM

Was This Post Helpful? 1
  • +
  • -

#4 phpa80   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-July 11

Re: create an array of numbers from numbers stored in textfile.

Posted 05 July 2011 - 01:01 AM

View Postfixo, on 04 July 2011 - 01:15 PM, said:

See if this will work for you

thanks for useful response.it worked. What should I do if i had floating numbers instead integers?
Was This Post Helpful? 0
  • +
  • -

#5 phpa80   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-July 11

Re: create an array of numbers from numbers stored in textfile.

Posted 05 July 2011 - 01:14 AM

i found solution.
 double a, b, c;
                    a=Convert.ToDouble(spl[0]);
                    b = Convert.ToDouble(spl[1]);
                    c = Convert.ToDouble(spl[2]);
                    double result = a + b + c;//<--your formula here
                    //MessageBox.Show(string.Format("A = {0}, B = {1}, C = {2}\nA + B + C = {3}", a, b, c, result));
                    StreamWriter sw = File.AppendText("C:\\Users\\Mehdi\\Documents\\Visual Studio 2008\\Projects\\.output.txt");
                    sw.WriteLine(result);
                    sw.Close();



is there any better idea ?
Was This Post Helpful? 1
  • +
  • -

#6 fixo   User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: create an array of numbers from numbers stored in textfile.

Posted 05 July 2011 - 04:01 AM

It's good enough,so no worries about
You might be want to use 'decimal.TrtyParse' method
for floating numbers as well as I've showed above
But no problem
Cheers :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1