Help writing function please,

T

tonyjeffs

In the following code, what type is examplefile?
I know that FILE is incorrect!
Thanks
Tony

main ()
{
char infile[]="test.it";
ifstream examplefile (infile);
myFunction (examplefile);
return 0;
}

myfunction(FILE ef)
{
/*do something with ef*/
......
.....
}
 
K

Ken Human

In the following code, what type is examplefile?
I know that FILE is incorrect!
Thanks
Tony

main ()
{
char infile[]="test.it";
ifstream examplefile (infile);
myFunction (examplefile);
return 0;
}

myfunction(FILE ef)
{
/*do something with ef*/
.....
....
}

examplefile is an object of type ifstream. Think of it this way, when
you declare a variable or object, you type: variable_type variable_name;
Compare it to the line above of where you declare examplefile, you
typed: char infile[] = "test.it"; A variable of type char named infile,
the [] tells us that it's an array, the = says that we're going to
define infile right now, and "test.it" is what we're defining infile as.

Your code should look more like:

#include <fstream>
#include <iostream>
using namespace std;

void myFunction(ifstream&);

int main() {
/*char infile[]="test.it"; this isn't necessary*/
ifstream examplefile("test.it");
myFunction(examplefile);
examplefile.close();
return 0;
}

void myFunction(ifstream& ef) {
/*do stuff to the stream, probably read it*/
while(ef.good()) cout << (char)ef.get();
cout << endl;
}

HTH
 
J

Jonathan Mcdougall

In the following code, what type is examplefile?
I know that FILE is incorrect!

You don't seem to understand how and object is defined. You should
back up and learn the basics.

main() returns an int. Always.

int main()
{
char infile[]="test.it";
ifstream examplefile (infile);

Make sure you included <fstream> and replace this with

std::ifstream examplefile(infile);
myFunction (examplefile);
return 0;
}
myfunction(FILE ef)

This is illegal in C++. A function must have a return value. Since
you want myfunction() to operate on an std::ifstream, just use that
type:

void myfunction(std::ifstream &if)

Note that streams cannot be copied so you'll have to use references or
pointers.


Jonathan
 
T

tonyjeffs

Thanks to all...
I had got close, but I left out the ampersand.
so... "ifstream = the address of the 'if' "
- is that a reasonable way of thinking about it?


The truest comment is Jonathan's:
"You should back up and learn the basics."
I know, I know. I'm itching to successfully write something more
ambitious,as a kind of reward, but I realise I'm not ready; so back to
the textbook next week.
Tony


Jonathan said:
In the following code, what type is examplefile?
I know that FILE is incorrect!

You don't seem to understand how and object is defined. You should
back up and learn the basics.

main() returns an int. Always.

int main()
{
char infile[]="test.it";
ifstream examplefile (infile);

Make sure you included <fstream> and replace this with

std::ifstream examplefile(infile);
myFunction (examplefile);
return 0;
}
myfunction(FILE ef)

This is illegal in C++. A function must have a return value. Since
you want myfunction() to operate on an std::ifstream, just use that
type:

void myfunction(std::ifstream &if)

Note that streams cannot be copied so you'll have to use references or
pointers.


Jonathan
 
B

ben

Thanks to all...
I had got close, but I left out the ampersand.
so... "ifstream = the address of the 'if' "
- is that a reasonable way of thinking about it?

More advices here:

1. Don't use "if" for the argument, it is a C++ keyword!
2. ifstream is a type, Jonathan's "if" is an object. A type is not an
object, just like a fruit is not an apple.
3. Unless you are using pointers, you don't have to worry about the address.

Given

void myfunction(std::ifstream &inputFile);

Think in this way:

This is a declaration of function named myfunction which, takes only one
parameter named inputFile which is a reference to an object of type
std::ifstream, and returns nothing.
 
T

tonyjeffs

Got it.
Hey I've just written a little program to find the size of a bitmap
file, passing a reference to ifstream to a function .
It works, and it's the first complicated (for me) program I've actually
'written' - without copying code wholesale. And whether or not it's
the best way, it's structured the way I planned it to be. *Thanks
again for help with the detail. *
Encouraged by success, I feel I'm actually getting somewhere.
Now I can get back to working through Accelerated C++.
(Maybe tomorrow)

Happy day!
Tony
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top