Help: ifstream not getting passed to a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francis Bell

    Help: ifstream not getting passed to a function

    I just found that my fin stream is not getting passed to my
    readInASpinnerb ait function. Here's what I have:

    string readInFirstChar s(ifstream &fin)
    {
    char first[2];
    string print;
    while (fin.good())
    {
    fin.get(first, 4, '/');
    if (strcmp(first, "sp")==0) {
    cout << first << endl; // debugging: outputs 'sp'
    string print = "spinnerbai t";
    readInASpinnerb ait(fin); // fin not getting passed
    fin.ignore(80, '\n');
    return print;
    }
    }
    return 0; }

    void readInASpinnerb ait(ifstream &fin)
    {
    cout << "it should read 'sp' right after this" << endl; //it doesn't
    char first[2];
    string print;
    fin.get(first, 4, '/');
    cout << first << endl;
    // debugging: it doesn't output 'sp'...so fin is not getting
    passed.
    Spinnerbait spinnerbaitLure ;
    SpinnerbaitList spinList;
    spinnerbaitLure .inputSpinnerba it(fin);
    while (!fin.fail()) {
    spinList.insert Spinnerbait(spi nnerbaitLure, 0);
    spinnerbaitLure .inputSpinnerba it(fin);
    }
    }

    I don't understand why fin wouldn't be getting passed here. This seems
    simple enough. On a whim, I even tried changing that to istream &sin.
    No go. I'd appreciate ANY input. Thanks!

    Frank

  • bartek

    #2
    Re: Help: ifstream not getting passed to a function

    Francis Bell <phrankndonna@c harter.net> wrote in news:10b1l5rcmn ijvd6
    @corp.supernews .com:
    [color=blue]
    > I just found that my fin stream is not getting passed to my
    > readInASpinnerb ait function. Here's what I have:[/color]

    What do you mean by "not getting passed"? Do you mean, that the argument
    'sinks' between the function call?

    The problem lies in the code you haven't posted, I suppose.

    Cheers.

    Comment

    • John Harrison

      #3
      Re: ifstream not getting passed to a function


      "Francis Bell" <phrankndonna@c harter.net> wrote in message
      news:10b1l5rcmn ijvd6@corp.supe rnews.com...[color=blue]
      > I just found that my fin stream is not getting passed to my
      > readInASpinnerb ait function. Here's what I have:
      >
      > string readInFirstChar s(ifstream &fin)
      > {
      > char first[2];[/color]

      char first[4];
      [color=blue]
      > string print;
      > while (fin.good())
      > {
      > fin.get(first, 4, '/');[/color]

      You are reading upto four characters in here, so you must declare first as
      having four characters.
      [color=blue]
      > if (strcmp(first, "sp")==0) {
      > cout << first << endl; // debugging: outputs 'sp'
      > string print = "spinnerbai t";
      > readInASpinnerb ait(fin); // fin not getting passed
      > fin.ignore(80, '\n');
      > return print;
      > }
      > }
      > return 0; }
      >
      > void readInASpinnerb ait(ifstream &fin)
      > {
      > cout << "it should read 'sp' right after this" << endl; //it doesn't
      > char first[2];[/color]

      Ditto, char first[4];
      [color=blue]
      > string print;
      > fin.get(first, 4, '/');
      > cout << first << endl;
      > // debugging: it doesn't output 'sp'...so fin is not getting
      > passed.
      > Spinnerbait spinnerbaitLure ;
      > SpinnerbaitList spinList;
      > spinnerbaitLure .inputSpinnerba it(fin);
      > while (!fin.fail()) {
      > spinList.insert Spinnerbait(spi nnerbaitLure, 0);
      > spinnerbaitLure .inputSpinnerba it(fin);
      > }
      > }
      >
      > I don't understand why fin wouldn't be getting passed here. This seems
      > simple enough. On a whim, I even tried changing that to istream &sin.
      > No go. I'd appreciate ANY input. Thanks!
      >[/color]

      It should be istream&, but that not the problem.

      I see you are using a string (the variable print), why don't you use a
      string instead of an array for first? Te big advantage of strings is that
      they automatically grow, you don't have to say how big they are, so you
      can't make the mistake you've made with first. The other advantage is that
      you can compare strings with ==, you don't have to use strcmp.

      string first;
      getline(first, '/');
      if (first == "sp")

      Very simple.

      john


      Comment

      • John Harrison

        #4
        Re: ifstream not getting passed to a function

        >[color=blue]
        > string first;
        > getline(first, '/');[/color]

        getline(fin, first, '/');
        [color=blue]
        > if (first == "sp")
        >[/color]

        Sorry.

        john


        Comment

        • Jorge Rivera

          #5
          Re: Help: ifstream not getting passed to a function

          Francis Bell wrote:[color=blue]
          > I just found that my fin stream is not getting passed to my
          > readInASpinnerb ait function. Here's what I have:[/color]

          This is impossible (or so I would like to think...).
          What do you mean by "doesn't get passed"?
          [color=blue]
          > string print;
          > fin.get(first, 4, '/');
          > cout << first << endl; // debugging: it doesn't output
          > // 'sp'...so fin is not getting passed.[/color]

          I am suspicious about your logic. You read "sp" before sending fin to
          the function. Then you expect to read it again?????

          After read, the internal pointer is moved forward. Hence consecutive
          calls to ifstream::read will not give you the same result, unless you
          have a repeated string....

          What I mean, unless your input is "spsp", and you call read with 2
          instead of 4 as your argument, you shouldn't get back "sp" on
          consecutive reads.

          JLR

          Comment

          • Francis Bell

            #6
            Re: Help: ifstream not getting passed to a function

            Jorge Rivera wrote:[color=blue]
            > Francis Bell wrote:
            >[color=green]
            >> I just found that my fin stream is not getting passed to my
            >> readInASpinnerb ait function. Here's what I have:[/color]
            >
            >
            > This is impossible (or so I would like to think...).
            > What do you mean by "doesn't get passed"?
            >[color=green]
            > > string print;
            > > fin.get(first, 4, '/');
            > > cout << first << endl; // debugging: it doesn't output
            > > // 'sp'...so fin is not getting passed.[/color]
            >
            > I am suspicious about your logic. You read "sp" before sending fin to
            > the function. Then you expect to read it again?????
            >
            > After read, the internal pointer is moved forward. Hence consecutive
            > calls to ifstream::read will not give you the same result, unless you
            > have a repeated string....
            >
            > What I mean, unless your input is "spsp", and you call read with 2
            > instead of 4 as your argument, you shouldn't get back "sp" on
            > consecutive reads.
            >
            > JLR[/color]
            Thanks everyone for your input. I was doing it completely wrong. I was
            treating like something like an integer or the like that can be passed
            by value or reference. I've still got a problem with 'data flow'
            somewhere that I haven't been able to trace, but I'm going to look at it
            a little more before trying to post that here; it involves several
            functions. Thanks all for your help!

            Frank

            Comment

            Working...