Question

I am making an console application for a 3D CAD program called Soplid Edge. With this application i let Visul Basic draw curves and lines in Solid Edge. To make the curves i calculate points, what get stored into an array, with an 4th degree equation. Now the problem. I read my data from a text file. But this text file has srveral lines for different curves. And my code read trough them all and takes the last line what has data in it. My question: How can i let my piece of code read the first line, calculate the points, make the curve and then does the same thing with the next line with data.

Here is my code that i want to get repeated for every line in my text file:

    'Dmax(Array)
    Dim listofdata As New ArrayList
    For x = (0.2 * QT) To ((QFACTOR + 0.1) * QT) Step (0.1 * QT)
        listofdata.Add(x)
        y = (((x ^ 4) * C1) + ((x ^ 3) * C2) + ((x ^ 2) * C3) + (x * C4) + C5)
        listofdata.Add(y)
    Next
    Dim dataArray() As Double
    dataArray = DirectCast(listofdata.ToArray(GetType(Double)), Double())

            ' Creating a Curve2d object by using the above defined points
    objCurves.AddByPoints(PointCount:=14, Points:=dataArray)

And this is how i read my file:

Console.Write("Path to file:")
Dim strFileName As String
strFileName = Console.ReadLine
Dim objFS As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
Dim objSR As New StreamReader(objFS)
Was it helpful?

Solution

With .Net Framework 4.x you can use ReadLines
http://msdn.microsoft.com/en-us/library/dd383503.aspx

    For Each line In IO.File.ReadLines(filename)
        ' do something with the string
    Next

ReadLines is an IEnumerable of String, which means that it will read one line after the other. If you use a version where this is not available, you simply replace it with ReadAllLines. ReadAllLines will read all lines at once into an array and then you are iterating over this array. If your file is small there is not much difference between both versions, but if you work with larger files, the first one has a smaller memory footprint and processing starts after reading the first and not when all lines have been read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top