Hello@all
I'm quite a C#-beginner. Currently, I'm working on an application for evaluating light measurements. That means, I have to read simple text files containing measurement-data in the following manner:
;MEASUREMENT
;Wave Value
340 5.48517
341 5.7362
342 6.04807
343 6.31996
344 6.68566
and so on...
My application needs only the Value-component which should be stored into a double array for further calculations. Basically, the whole thing should work something like this (if I'm thinking right)
1. Open a text file using the FileOpen-Dialog
2. Open a Streamreader to read the selected file
3. Ignore anything that is not a number
4. Ignore the Wave-values
5. Store the rest into an string array and convert it to double
I would be grateful for any hints.
Thanks in advance
Vladimir
Loading
VulpesPosted Sep 12, 2013, 11:47 AM
Vladimir GajicPosted Sep 13, 2013, 3:41 AM
Vladimir GajicPosted Sep 12, 2013, 10:56 AM
one small problem still persists. I used the Linq-Approach posted by Wulpes. It works great as long as my system language setting is set to "en-US". I also managed some globalization so that the application can run on various language-settings. So far so good. Problems occur when my textfile is generated on a system witch uses a decimal colon rather than a decimal point. In that case my calculation is rubbish. Therefore I would like to check the input text file and replace eventual colons by dec. points.
Is there a way to implement this task to your Linq-Code, Wulpes?
Thanks in advance
Vladimir GajicPosted Aug 21, 2013, 12:08 PM
that worked great. The reason for the exception was the decimal separator witch was a colon instead of decimal point. This could be handled without much problems.
Thanks a lot. From this point I'll explore further but I'm quite shure that questions will arise ;)
Regards
Vladimir
VulpesPosted Aug 20, 2013, 11:31 AM
You'll then get an 'index out of range' error.
My approach integrated into Iftikar's OpenFileDialog code is as follows:
Vladimir GajicPosted Aug 20, 2013, 11:20 AM
Regards
Vladimir
Iftikar HussainPosted Aug 20, 2013, 11:15 AM
Regards,
Iftikar
Vladimir GajicPosted Aug 20, 2013, 11:03 AM
Vladimir GajicPosted Aug 20, 2013, 9:15 AM
I'll try the second one, just have to figure-out how to combine with a openFile-dialog.
I'll report.
Regards
Vladimir GajicPosted Aug 20, 2013, 7:47 AM
Regards
Vladimr
VulpesPosted Aug 20, 2013, 7:16 AM
.NET 4.0 or later required:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
string filePath = "measurements.txt";
string[] separators = new string[]{" "};
double[] values = File.ReadLines(filePath).Skip(2)
.Select(s => double.Parse(s.Split(separators, StringSplitOptions.RemoveEmptyEntries)[1]))
.ToArray();
foreach(double value in values) Console.WriteLine(value);
Console.ReadKey();
}
}
The output is:
Vladimir GajicPosted Aug 20, 2013, 7:13 AM
Regards
Vladimir
Iftikar HussainPosted Aug 20, 2013, 7:03 AM
Regards,
Iftikar
Vladimir GajicPosted Aug 20, 2013, 6:41 AM
yes, the text file will allways be in that format since it comes from a singular measurement device.
Regards
Vladimir
Iftikar HussainPosted Aug 20, 2013, 6:37 AM
Is your textfile will be always content in this format?
Regards,
Iftikar