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

VulpesRank #55 · 29.2K reputationPosted Sep 12, 2013, 7:47 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 20, 2013, 2:37 AM EDT
Is your textfile will be always content in this format?
Regards,
Iftikar
Vladimir Gajic57 reputationPosted Aug 20, 2013, 2:41 AM EDT
yes, the text file will allways be in that format since it comes from a singular measurement device.
Regards
Vladimir
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 20, 2013, 3:03 AM EDT
Regards,
Iftikar
Vladimir Gajic57 reputationPosted Aug 20, 2013, 3:13 AM EDT
Regards
Vladimir
VulpesRank #55 · 29.2K reputationPosted Aug 20, 2013, 3:16 AM EDT
.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 Gajic57 reputationPosted Aug 20, 2013, 3:47 AM EDT
Regards
Vladimr
Vladimir Gajic57 reputationPosted Aug 20, 2013, 5:15 AM EDT
I'll try the second one, just have to figure-out how to combine with a openFile-dialog.
I'll report.
Regards
Vladimir Gajic57 reputationPosted Aug 20, 2013, 7:03 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 20, 2013, 7:15 AM EDT
Regards,
Iftikar
Vladimir Gajic57 reputationPosted Aug 20, 2013, 7:20 AM EDT
Regards
Vladimir
VulpesRank #55 · 29.2K reputationPosted Aug 20, 2013, 7:31 AM EDT
You'll then get an 'index out of range' error.
My approach integrated into Iftikar's OpenFileDialog code is as follows:
Vladimir Gajic57 reputationPosted Aug 21, 2013, 8:08 AM EDT
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
Vladimir Gajic57 reputationPosted Sep 12, 2013, 6:56 AM EDT
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 Gajic57 reputationPosted Sep 12, 2013, 11:41 PM EDT