473,395 Members | 1,616 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,395 software developers and data experts.

Passing an ifstream object to another function

Hello,

I wonder if I could pick your brains. I'm beginning to learn about C++. I
have opened a file in my program and I want to read lines from it. I would
like this to be done in a separate function called readline() because I
would also like to do some processing on the line each time (ignoring
comments and so on).

I have:

void Fileloader::readline(char *string, ifstream file) {
file.getline(string, sizeof(string), '\n');
}
And in another function I have:

ifstream file;
file.open("myfile.txt", ios::in);
char string[512];
readline(string, file);

To call my readline file.

This comes up with lots of errors like:
/usr/include/c++/3.3.1/bits/ios_base.h:668: error:
`std::ios_base::ios_base(const std::ios_base&)' is private
fileloader.cpp:62: error: within this context
Can anyone see what I'm doing wrong and point me in the right direction?

Thanks very much,
Tom
Jul 22 '05 #1
6 4237
"csvka" <to*@tomdavis.org.uk> wrote...
I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate function called readline() because I
would also like to do some processing on the line each time (ignoring
comments and so on).

I have:

void Fileloader::readline(char *string, ifstream file) {
Make it

voif Fileloader::readline(string& str, ifstream& file)
file.getline(string, sizeof(string), '\n');


'sizeof' is not good here (even when your 'string' was a pointer).
It never gives the size of the array behind the pointer.

V
Jul 22 '05 #2
csvka wrote:
Hello,

I wonder if I could pick your brains. I'm beginning to learn about C++. I
have opened a file in my program and I want to read lines from it. I would
like this to be done in a separate function called readline() because I
would also like to do some processing on the line each time (ignoring
comments and so on).

I have:

void Fileloader::readline(char *string, ifstream file) {
Try passing ifstream by reference :

void Fileloader::readline(char *string, ifstream & file) {
file.getline(string, sizeof(string), '\n');
}
And in another function I have:

ifstream file;
file.open("myfile.txt", ios::in);
char string[512];
readline(string, file);

To call my readline file.

This comes up with lots of errors like:
/usr/include/c++/3.3.1/bits/ios_base.h:668: error:
`std::ios_base::ios_base(const std::ios_base&)' is private
fileloader.cpp:62: error: within this context
Can anyone see what I'm doing wrong and point me in the right direction?


You can't make copies of ifstream.

The other thing is that you probably don't care about the "f" or file
semantics of a stream.

So this below is better.

void Fileloader::readline(char *string, istream & file)

But wait - there's more - sizeof( string ) will return the sizeof a
char * (which is probably 4 on your machine) which is probably NOT what
you want.

You also don't really want to use char *, try and see if std::string
works for you.

Jul 22 '05 #3

"csvka" <to*@tomdavis.org.uk> wrote in message
news:c0**********@wisteria.csv.warwick.ac.uk...
Hello,

I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate function called readline() because I would also like to do some processing on the line each time (ignoring comments and so on).

I have:

void Fileloader::readline(char *string, ifstream file) {
file.getline(string, sizeof(string), '\n');
}
Here you seem to want to provide readline as an alias for getline.
Why not just
use getline? Is there any reason readline must be a member function?
It doesn't use any of the calling object's data.

But more important than that, don't use std::istream::getline (char
*, int, char)!
Instead use std::getline (std::istream &, std::string &), which is
in the standard
<string> header. The version of getline which takes a
character-array as a
buffer is unsafe unless used properly (which you haven't - the
length of the
null-terminated sequence of characters pointed to by "string" is
unlikely to
be a good buffer size for getline) because the buffer can be
overrun. A
corollary of this advice is that when you want a string, use
std::string, not
character-arrays, because it handles the buffer's allocation and
deallocation
automatically, can be safely copied, etc.

Taking this on board, in your program, where previously you had:
object.readline (array, stream); // for 'object' declared as a
Fileloader instance
you will now want:
std::getline (s, stream); // where s is an std::string object.

And in another function I have:

ifstream file;
file.open("myfile.txt", ios::in);
char string[512];
readline(string, file);

To call my readline file.

This comes up with lots of errors like:
/usr/include/c++/3.3.1/bits/ios_base.h:668: error:
`std::ios_base::ios_base(const std::ios_base&)' is private
fileloader.cpp:62: error: within this context
std::ios_base is a base class of std::ifstream, and the copy
constructor
of std::ios_base is private, so std::ifstream objects cannot be
copied.
You would need to pass the stream by reference, as:

void Fileloader::readline(char *string, std::ifstream & file);

But seriously, use std::string and std::getline (std::istream &,
std::string &)
instead.
Can anyone see what I'm doing wrong and point me in the right direction?
Thanks very much,
Tom


Good luck and best regards,
Buster.
Jul 22 '05 #4

"csvka" <to*@tomdavis.org.uk> wrote in message
news:c0**********@wisteria.csv.warwick.ac.uk...
Hello,

I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate function called readline() because I
would also like to do some processing on the line each time (ignoring
comments and so on).

I have:

void Fileloader::readline(char *string, ifstream file) {
file.getline(string, sizeof(string), '\n');
}
You're just learning so you are making all the classic mistakes.

Pass streams by reference.

Use istream not ifstream in function. Why do you care what sort of stream it
is, as long as you can read from it who cares. If you say ifstream you are
saying I only want to read from a file, if you say istream you are saying I
want to read from anything, so your code is more flexible.

Don't use char*, use string instead. Especially don't use sizeof on char*
because IT WONT WORK!!! (The answer is usually 4, which is the size of a
char* pointer).

Putting all that together we have

void Fileloader::readline(string& str, istream& input) {
getline(input, str, '\n');
}

which is how you should write that function.


And in another function I have:

ifstream file;
file.open("myfile.txt", ios::in);
char string[512];
readline(string, file);


Right change this to

ifstream file("myfile.txt", ios::in);
string str;
readline(str, file);

No need to call open, you can declare the variable and open the file in one
go.

But most importantly, you've replaced the char array (which was limited to
512) with a string which is unlimited! You need to include the header
<string> (no .h in that) to get the string class.

You'll make life much easier for yourself learning C++ if you get into good
habits right away. There a lot of different ways to go wrong in C++. Most
importantly you need a good book, which one are you using?

john

Jul 22 '05 #5
John Harrison wrote:

[snipped]

No need to call open, you can declare the variable and open the file in
one go.

But most importantly, you've replaced the char array (which was limited to
512) with a string which is unlimited! You need to include the header
<string> (no .h in that) to get the string class.
Thank you. :-) That was really helpful.

You'll make life much easier for yourself learning C++ if you get into
good habits right away. There a lot of different ways to go wrong in C++.
Most importantly you need a good book, which one are you using?

Well, I've done a bit of Java and a bit of C before. I've picked up the
O'Reilly book "Practical C++ Programming". Can you recommend any other
good ones?

Thanks again for the help,

Tom
(csvka in the other post)
Jul 22 '05 #6

"Tom Davis" <ne**@NOSPAM-tomdavis-PLEASE.org.uk> wrote in message
news:c0**********@wisteria.csv.warwick.ac.uk...
John Harrison wrote:

[snipped]

No need to call open, you can declare the variable and open the file in
one go.

But most importantly, you've replaced the char array (which was limited to 512) with a string which is unlimited! You need to include the header
<string> (no .h in that) to get the string class.


Thank you. :-) That was really helpful.

You'll make life much easier for yourself learning C++ if you get into
good habits right away. There a lot of different ways to go wrong in C++. Most importantly you need a good book, which one are you using?

Well, I've done a bit of Java and a bit of C before. I've picked up the
O'Reilly book "Practical C++ Programming". Can you recommend any other
good ones?

Thanks again for the help,

Tom
(csvka in the other post)


If you have previously programmed before then the one that is normally
recommended by this group is Accelerated C++ by Koenig and Moo.

The one you are reading has a poor review here

http://www.accu.org/cgi-bin/accu/rvo...&file=p001010a

The author is accused of simply translating a C style into C++, since you
are used to C that would be a problem for you. Also it cannot be denied that
the book is fairly old. C++ has moved on a lot in recent years.

john
Jul 22 '05 #7

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

Similar topics

2
by: Ryan Malone | last post by:
Passing Dictionary object byref Ive created an ASP class that uses a dictionary object which is filled from a recordset. It passes the object to the propterty of another ASP class byref: ...
4
by: andy | last post by:
i am coding a dll which contains a function take a const wchar_t* as parameter, call it form a exe, the wchar_t is passed to dll function, but the function in dll will then call another function...
6
by: Ian Robertson | last post by:
I am trying to write a function that takes a reference to an object as its arguement. The object is created in another function and I am trying to pass the object to another function from within...
4
by: Ken | last post by:
Hello I am trying to change the color of a font in a text box. I have been reading about and trying various examples found it this group, but still can't get it right. Here is where I am...
4
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double...
2
by: Ian Partridge | last post by:
Hi, I want to write a varargs function which then passes its parameters to another varargs function. I have RTFFAQ: http://www.eskimo.com/~scs/C-faq/q15.12.html which says that the other...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
4
by: Studlyami | last post by:
Okay i create a class object in my "main" class. I want to pass this object by reference to another class. I am able to pass the ref of the object to the constructor, but i don't know how i am able...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.