473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help writing function please,

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*/
......
.....
}

Jul 23 '05 #1
6 1343
Dear Tony,

to*******@aol.com writes:
In the following code, what type is examplefile?
I know that FILE is incorrect! ifstream examplefile (infile);
myFunction (examplefile); myfunction(FILE ef)


Examplefile is of type ifstream. FILE should be ifstream&. I.e., a reference
to an ifstream.

Good luck,
Chris
Jul 23 '05 #2
to*******@aol.com wrote:
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
Jul 23 '05 #3
> 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 ()
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

Jul 23 '05 #4
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 Mcdougall wrote:
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 ()


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


Jul 23 '05 #5
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.
Jul 23 '05 #6
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

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
13
by: Chiller | last post by:
I'm now getting close to finishing my Distance class. In the code below I have included a number of overload operators that test for equality etc. I've also added more code in the TEST_DISTANCE...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
1
by: Rizwan | last post by:
I am writing this code for file upload in asp 3.0 on my webpage........ It is working on local system but when i want to upload an image on my website from client machine it is not working ..........
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.