Reputation: 147
i am trying to parse through several Files, while doing so there are some values to ignore and so on. I currently did it with this Code:
double[][] rows = File
.ReadLines(filepath)
.Select(line => line
.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase)))
.Where(items => items.Any())
.Select(items => items
.Select(item => double.Parse(item, CultureInfo.InvariantCulture))
.ToArray())
.ToArray();
But i recently got the Error. After searching in the Internet i found out that
using (FileStream stream = File.Open("path to file", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{ DO SOMETHING}
}
}
Would fix this problem. However i can´t manage to change that Code to my needs. When i try:
using (FileStream stream = File.Open("path to file", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
reader.ReadLine().Select(line => line
.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase)))
.Where(items => items.Any())
.Select(items => items
.Select(item => double.Parse(item, CultureInfo.InvariantCulture))
.ToArray())
.ToArray();
}
}
}
It gives me an error on ".Split" since its not a known definition for a StreamReader... I know this maight be a really silly questions and pretty easy to solve but i can´t manage it to be done some how...
Thanks in Advance
Regards
Upvotes: 1
Views: 1707
Reputation: 1215
Reader.ReadLine()
will return a string. So when you do .Select
on this you are projecting this into an IEnumerable<char>
. You cannot then do .Split
on a char.
You will need to reformat your code to work by reading one line at a time
So something line this:
reader.ReadLine()
.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase))
.Where(items => items.Any())
.Select(items => items.Select(item => double.Parse(item.ToString())).ToArray()).ToArray();
Update
To answer your comment I would most probably change the data structure from double[][]
to a List<List<double>>
If you want to keep the double[][]
you can do something like this.
You can change the code to add to the list while streaming the file:
List<double[]> example = new List<double[]>();
while (!reader.EndOfStream)
{
example.Add(reader.ReadLine()
.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase))
.Where(items => items.Any())
.Select(item => double.Parse(item.ToString(), CultureInfo.InvariantCulture)).ToArray());
}
var returnvalue = example.ToArray();
Note
When making changes like this make sure that the results match the original code.
Upvotes: 1
Reputation: 437
Is it neccessary to use outer "using"? Try this:
try
{
using (var reader = new StreamReader(filename))
{
while (!reader.EndOfStream)
{
{ your split implementation }
}
}
}
// now you dont need to close a stream because
// using statement will handle it for you
catch // appropriate exception handling
{
}
Upvotes: 0
Reputation: 819
You need to replace reader.ReadLine()
with reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None)
. Also you can write own your extension method ReadLines
for the reader class.
Upvotes: 0