streamreader position question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Cummings

    #1

    streamreader position question

    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,12 5,180

    //sand old growth
    2
    abiebals,100
    betualle,100,12 5,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(I S);
    position = inFile.position ;
    line = inFile.readLine ();

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

  • Jed Ozone

    #2
    Re: streamreader position question

    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).


    "Bob Cummings" <rancho@dwave.n et> wrote in message
    news:emxBLpAvDH A.2492@TK2MSFTN GP12.phx.gbl...[color=blue]
    > 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,12 5,180
    >
    > //sand old growth
    > 2
    > abiebals,100
    > betualle,100,12 5,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(I S);
    > position = inFile.position ;
    > line = inFile.readLine ();
    >
    > if (line != null)
    > {
    > fields = line.Split(',') ;
    > if(fields.Lengt h ==1)// reset the stream position
    > {
    > inFile.position = position; }
    > }// end if end of file
    > }
    >[/color]


    Comment

    • Bob Cummings

      #3
      Re: streamreader position question

      Jed Ozone wrote:[color=blue]
      > 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).
      >
      >
      > "Bob Cummings" <rancho@dwave.n et> wrote in message
      > news:emxBLpAvDH A.2492@TK2MSFTN GP12.phx.gbl...[/color]


      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.




      [color=blue]
      >[color=green]
      >>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,5 0,78
      >>acerrubr,10 0
      >>betualle,100, 125,180
      >>
      >>//sand old growth
      >>2
      >>abiebals,10 0
      >>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;
      >>InitialSpecie s IS = new InitialSpecies( );
      >>IS.build(fiel ds);
      >>mySpecies.Add (IS);
      >>position = inFile.position ;
      >>line = inFile.readLine ();
      >>
      >>if (line != null)
      >>{
      >> fields = line.Split(',') ;
      >>if(fields.Len gth ==1)// reset the stream position
      >>{
      >>inFile.positi on = position; }
      >>}// end if end of file
      >>}
      >>[/color]
      >
      >
      >[/color]

      Comment

      Working...