2 Replies - 4137 Views - Last Post: 16 October 2013 - 10:21 AM

#1 Xakzyy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-October 13

C++/C# Reading interger from File, do some calculate, write to Textbox

Posted 15 October 2013 - 05:00 PM

Hello!

I code in C++\CLR Windows Form Application (Answer can be in C# Windows Form)

I need help with reading a .txt file that contains 3 values of interger.

Text file example:

0.625"0.04"1


I know how to read them as string and write out to textbox by using this code:

String^ readfiletest = "Testfile.txt";
				 try
				 {
					 StreamReader^ DataIn = File::OpenText(readfiletest);
					 String^ DataStr;
					 int count = 0;
					 array<String^>^ result;
					 array<Char>^ separ = gcnew array<Char>{'"'};

					 while((DataStr = DataIn->ReadLine()) != nullptr)
					 {
						 count++;
						 result = DataStr->Split(separ);
						 ...
						 ...
						 ...


Which reads everything in file and gives them a value of "result[x]".

What I need help with is to read a text file, make the following string into interger, do some calculating
(Example on how I will calculate things from previous mention of textfile, 0.625 / (1 - 0.04) )

After calculating it I will write the answer in a textbox.

Really appriciate the help..

Is This A Good Question/Topic? 0
  • +

Replies To: C++/C# Reading interger from File, do some calculate, write to Textbox

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6996
  • View blog
  • Posts: 23,770
  • Joined: 05-May 12

Re: C++/C# Reading interger from File, do some calculate, write to Textbox

Posted 15 October 2013 - 06:18 PM

Well, you'll be in for a really hard time because the three numbers on the line, only the 1 is an integer and the first two numbers are either floating point numbers in C/C++/C# or decimal in C#.

Anyway, using the TryParse or Parse() method of your target types should get you what you need.
Was This Post Helpful? 0
  • +
  • -

#3 Xakzyy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-October 13

Re: C++/C# Reading interger from File, do some calculate, write to Textbox

Posted 16 October 2013 - 10:21 AM

View PostSkydiver, on 15 October 2013 - 06:18 PM, said:

Well, you'll be in for a really hard time because the three numbers on the line, only the 1 is an integer and the first two numbers are either floating point numbers in C/C++/C# or decimal in C#.

Anyway, using the TryParse or Parse() method of your target types should get you what you need.


Thanks for your answer, as I have searched for and how to use tryparse / parse () I don't think I can use it with the code I use to open & split the text in the text file..

let me show you what I have coded so far and what error I get.

string textfileopen = "New Folder\\testfile.txt";

            try
            {
                StreamReader DataIn = File.OpenText(textfileopen); // Opening testfile.txt
                string DataStr;
                int count = 0;
				
				//Test values of double
                double stringtodoubletest; 
                double stringtodoubletestanothernumber;
                double addingbothdoubletogether;
				//Test values of string
                string doubletostringtest;
				
                string[] result; //Give each split a value of result[x]
                char[] separ = new char[] { '"' }; // split words in text file

                while ((DataStr = DataIn.ReadLine()) != null)
                {
                    count++;
                    result = DataStr.Split(separ);

                    textBox1.Text = result[0]; //write out result to textbox
                    textBox2.Text = result[1];
                    textBox3.Text = result[2];

                    stringtodoubletest = Convert.ToDouble(result[0]); //convert result[0] to double
                    stringtodoubletestanothernumber = Convert.ToDouble(result[1]); //convert result[1] to double
					
					//Adding result[0] & result[1] into a test value
                    addingbothdoubletogether = stringtodoubletest + stringtodoubletestanothernumber;
					
					//test value (double) to string
                    doubletostringtest = Convert.ToString(addingbothdoubletogether);
					
					//write out test string to empty textbox
                    textBox4.Text = doubletostringtest;
                }
            }
            catch (InvalidCastException ex)
            {
                MessageBox.Show("error:" + ex.Message);
            }


Error is following --> "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

How should I edit this? sincerely~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1