Grasshopper

algorithmic modeling for Rhino

I know it isn´t really a GH question but I couldn´t find the answer out there and I´m in a little pinch. I´d like to read several files with the Streamreader method in a for loop formatting the string of the paths. The kind of "imported java syntax" doesn´t seem to work:

(C# code)

List <string> list = new List <string> ();

for (int month = 1; month <= 12; month++)
{
StreamReader reader = new StreamReader(String.Format("C:\blablabla\wind_speed_2007_{0}.txt", month));
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line); // Add to list.
}
}
A = list;


Anyone can help?

Thanks in advance.

Views: 1794

Replies to This Discussion

Just make multiple stream readers for each file that you want to bring in...

StreamReader stream_1 = new StreamReader ("C:\somewhere\stream1.txt")
//create as many other streams as you want...
StreamReader stream_2 = new StreamReader ("C:\somewhereElse\stream2.txt")
Thanks, Damien.

Actually, I have like 60 files to read, so I used loop not to have so many streamreaders. Do you know a way to format the file path?
lets say the file names are: stream0.txt, stream1.txt, etc...

you would wrap that in a for loop to create the stream reader...I'd then store that info in an array or something


PSEUDOCODE
Dim pathPre As String
Dim pathSuf As String

pathPre = "C:\somewhere\stream"
pathSuff = ".txt"


For i = 0 to 60 (number of files to read)
Dim path As String

path = pathPre & i & pathSuf
StreamReader stream = new StreamReader (path)
'other code here to extract the read data and store it somewhere
Next


This is not actual code, just what I would think to do in situation where the file name is more or less the same and just changes with a number incrementation...
Thanks a lot for your help, Luis.

It´s funny but it only worked, using x & y with the post-it panel as input strings and not directly from the code, as you can see in the image.

Cheers.
Attachments:
not sure why it does not work the other way...I do it all the time...and yes usually I would put the prefix and suffix in a panel for more flexibility. GLad it works!
I guess I misread the question. It seamed as though you wanted several streams with separate access so that you could read a line from one and a line from another.
There´s no problem, Damien. Thanks anyway.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service