Help: ifstream not getting passed to a function

Discussion in 'C++' started by Francis Bell, May 23, 2004.

  1. Francis Bell

    Francis Bell Guest

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

    string readInFirstChars(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 = "spinnerbait";
    readInASpinnerbait(fin); // fin not getting passed
    fin.ignore(80, '\n');
    return print;
    }
    }
    return 0; }

    void readInASpinnerbait(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.inputSpinnerbait(fin);
    while (!fin.fail()) {
    spinList.insertSpinnerbait(spinnerbaitLure, 0);
    spinnerbaitLure.inputSpinnerbait(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
     
    Francis Bell, May 23, 2004
    #1
    1. Advertising

  2. Francis Bell

    bartek Guest

    Francis Bell <> wrote in news:10b1l5rcmnijvd6
    @corp.supernews.com:

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


    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.
     
    bartek, May 23, 2004
    #2
    1. Advertising

  3. Re: ifstream not getting passed to a function

    "Francis Bell" <> wrote in message
    news:...
    > I just found that my fin stream is not getting passed to my
    > readInASpinnerbait function. Here's what I have:
    >
    > string readInFirstChars(ifstream &fin)
    > {
    > char first[2];


    char first[4];

    > string print;
    > while (fin.good())
    > {
    > fin.get(first, 4, '/');


    You are reading upto four characters in here, so you must declare first as
    having four characters.

    > if (strcmp(first, "sp")==0) {
    > cout << first << endl; // debugging: outputs 'sp'
    > string print = "spinnerbait";
    > readInASpinnerbait(fin); // fin not getting passed
    > fin.ignore(80, '\n');
    > return print;
    > }
    > }
    > return 0; }
    >
    > void readInASpinnerbait(ifstream &fin)
    > {
    > cout << "it should read 'sp' right after this" << endl; //it doesn't
    > char first[2];


    Ditto, char first[4];

    > 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.inputSpinnerbait(fin);
    > while (!fin.fail()) {
    > spinList.insertSpinnerbait(spinnerbaitLure, 0);
    > spinnerbaitLure.inputSpinnerbait(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!
    >


    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
     
    John Harrison, May 23, 2004
    #3
  4. Re: ifstream not getting passed to a function

    >
    > string first;
    > getline(first, '/');


    getline(fin, first, '/');

    > if (first == "sp")
    >


    Sorry.

    john
     
    John Harrison, May 23, 2004
    #4
  5. Francis Bell

    Jorge Rivera Guest

    Francis Bell wrote:
    > I just found that my fin stream is not getting passed to my
    > readInASpinnerbait function. Here's what I have:


    This is impossible (or so I would like to think...).
    What do you mean by "doesn't get passed"?

    > string print;
    > fin.get(first, 4, '/');
    > cout << first << endl; // debugging: it doesn't output
    > // 'sp'...so fin is not getting passed.


    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
     
    Jorge Rivera, May 23, 2004
    #5
  6. Francis Bell

    Francis Bell Guest

    Jorge Rivera wrote:
    > Francis Bell wrote:
    >
    >> I just found that my fin stream is not getting passed to my
    >> readInASpinnerbait function. Here's what I have:

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

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

    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
     
    Francis Bell, May 24, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    529
    Joseph Byrns
    Apr 15, 2005
  2. Anand
    Replies:
    2
    Views:
    925
    Anand
    Sep 11, 2003
  3. Les Caudle
    Replies:
    1
    Views:
    351
    Cowboy \(Gregory A. Beamer\)
    Sep 19, 2006
  4. Mastupristi
    Replies:
    1
    Views:
    401
    Jonathan Mcdougall
    Nov 9, 2005
  5. xz
    Replies:
    2
    Views:
    411
    James Kanze
    Mar 14, 2008
Loading...

Share This Page