By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Help: ifstream not getting passed to a function

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Francis Bell <ph**********@charter.net> 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.
Jul 22 '05 #2

P: n/a

"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...
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
Jul 22 '05 #3

P: n/a
>
string first;
getline(first, '/');
getline(fin, first, '/');
if (first == "sp")


Sorry.

john
Jul 22 '05 #4

P: n/a
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
Jul 22 '05 #5

P: n/a
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

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.