streamreader position question

  • Thread starter Thread starter Bob Cummings
  • Start date Start date
B

Bob Cummings

Greetings

I am stumped. I am trying to create some initial conditions object by
reading from a file. These objects have an integer ID and then a
list of sub objects. The problem is I will not know at run time how
many sub objects there may be. Here is an example of the input text file.

0
abiebals,20,50,78
acerrubr,100
betualle,100,125,180

//sand old growth
2
abiebals,100
betualle,100,125,180

What I would like to do is read the file line by line and split it into
a fields string array. Then if the size of my fields array is equal to
1 I will know it is time to build a new object. I would then like to
reset the stream object back to the position it had before it read the
integer and pass it back to the Controller class to begin the building
of another initial conditions object. But this does not seem to work.
Any suggestions ??

Here is what I have tried.

string [] fields;
string line = inFile.readLine();
fields = line.Split(',');

while (fields.Length>1)
{
long position;
InitialSpecies IS = new InitialSpecies();
IS.build(fields);
mySpecies.Add(IS);
position = inFile.position;
line = inFile.readLine();

if (line != null)
{
fields = line.Split(',');
if(fields.Length ==1)// reset the stream position
{
inFile.position = position; }
}// end if end of file
}
 
Why don't you just read the entire file into a string (unless it's huge,
which from below, I'm guessing it's not over 1 MB).

Split the string up based on newline. Then loop the array of strings and
when you hit a string of length 1, you know all the next indexes in the
array are your sub objects until you hit an index where the value is lenght
1 again.

Then as you hit each index after the integerID you just do another split on
that one based on comma.

Or use a regular expression (Regex) to break up the file into the pieces. I
just started using them, and to be honest, they're kind of a pain to get
used to. But in your case, I think it would be a pretty simple regular
expression (of course, I could be wrong about that).
 
Jed said:
Why don't you just read the entire file into a string (unless it's huge,
which from below, I'm guessing it's not over 1 MB).

Split the string up based on newline. Then loop the array of strings and
when you hit a string of length 1, you know all the next indexes in the
array are your sub objects until you hit an index where the value is lenght
1 again.

Then as you hit each index after the integerID you just do another split on
that one based on comma.

Or use a regular expression (Regex) to break up the file into the pieces. I
just started using them, and to be honest, they're kind of a pain to get
used to. But in your case, I think it would be a pretty simple regular
expression (of course, I could be wrong about that).


news:[email protected]...


I like your idea of reading the whole thing in at once. I will look
into the regular expressions. I have never heard of them before, but I
am sure I can find out.




Greetings

I am stumped. I am trying to create some initial conditions object by
reading from a file. These objects have an integer ID and then a
list of sub objects. The problem is I will not know at run time how
many sub objects there may be. Here is an example of the input text file.

0
abiebals,20,50,78
acerrubr,100
betualle,100,125,180

//sand old growth
2
abiebals,100
betualle,100,125,180

What I would like to do is read the file line by line and split it into
a fields string array. Then if the size of my fields array is equal to
1 I will know it is time to build a new object. I would then like to
reset the stream object back to the position it had before it read the
integer and pass it back to the Controller class to begin the building
of another initial conditions object. But this does not seem to work.
Any suggestions ??

Here is what I have tried.

string [] fields;
string line = inFile.readLine();
fields = line.Split(',');

while (fields.Length>1)
{
long position;
InitialSpecies IS = new InitialSpecies();
IS.build(fields);
mySpecies.Add(IS);
position = inFile.position;
line = inFile.readLine();

if (line != null)
{
fields = line.Split(',');
if(fields.Length ==1)// reset the stream position
{
inFile.position = position; }
}// end if end of file
}
 
Back
Top