Help: ifstream not getting passed to a function

F

Francis Bell

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
 
B

bartek

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

John Harrison

Francis Bell said:
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
 
J

Jorge Rivera

Francis said:
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
 
F

Francis Bell

Jorge said:
This is impossible (or so I would like to think...).
What do you mean by "doesn't get 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top