Want to pass a file name to a function

J

JoeC

I am writing a program and I would like to load a graphic file to a
constructor. The graphic stuff shouldn't matter, I want to pass a file
name to a constructor and create an object with the data in a
particular file so far I have:

graphic::graphic(std::string n){
std::ifstream fin("graphic.txt"); <-- I want to pass a string then
load that data into an object.
std::ifstream fin(n); <-- How can I make this work?
 
S

Sumit Rajan

JoeC said:
I am writing a program and I would like to load a graphic file to a
constructor. The graphic stuff shouldn't matter, I want to pass a file
name to a constructor and create an object with the data in a
particular file so far I have:

graphic::graphic(std::string n){
std::ifstream fin("graphic.txt"); <-- I want to pass a string then
load that data into an object.
std::ifstream fin(n); <-- How can I make this work?


std::ifstream fin(n.c_str());
 
I

Ivan Vecerina

:I am writing a program and I would like to load a graphic file to a
: constructor. The graphic stuff shouldn't matter, I want to pass a file
: name to a constructor and create an object with the data in a
: particular file so far I have:
:
: graphic::graphic(std::string n){
NB: the proper way of passing input strings by parameter is usually:
std::string const& n
[use a const reference to avoid an unnecessary copy].

: std::ifstream fin("graphic.txt"); <-- I want to pass a string then
: load that data into an object.
: std::ifstream fin(n); <-- How can I make this work?

std::ifstream fin( n.c_str() );

For some reason, the constructors of fstream take a C-style
string pointer instead of an std::string. You therefore
need to call the c_str() member of the string object to
obtain this C-style pointer.


hth -Ivan
 
M

Marc

JoeC said:
graphic::graphic(std::string n){
std::ifstream fin("graphic.txt"); <-- I want to pass a string then
load that data into an object.
std::ifstream fin(n); <-- How can I make this work?

What the exact error? I think it's probably expecting a char* C style string

See:

http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/classstd_1_1basic__ifstream.html

Constructor & Destructor Documentation
basic_ifstream ( ) [inline]

Default constructor.
Initializes sb using its default constructor, and passes &sb to the base
class initializer. Does not open any files (you haven't given it a filename
to open).
Definition at line 428 of file fstream.
References basic_ios::init().

basic_ifstream ( const char * __s,
ios_base::eek:penmode __mode = ios_base::in
) [inline, explicit]


et cetera.

Not very convenient, but you can for example use

n.c_str()

to get a c type string for a string.

Marcus Wentink
 
D

Daniel T.

"JoeC said:
I am writing a program and I would like to load a graphic file to a
constructor. The graphic stuff shouldn't matter, I want to pass a file
name to a constructor and create an object with the data in a
particular file so far I have:

graphic::graphic(std::string n){
std::ifstream fin("graphic.txt"); <-- I want to pass a string then
load that data into an object.
std::ifstream fin(n); <-- How can I make this work?

Others have answered your specific questions so I'm going to make a more
general critique...

I'm not sure that such a constructor is a good idea. Not all data comes
from files after all. Better might be something like this:

graphic::graphic( istream& is ) {
// use 'is' like you would 'fin'
}

Now you can load from things other than files, as long as they derive
from istream. Also, you can keep several graphics in one file and pass
it from one graphic object to another. This would come in handy for your
animation class (if you end up with one.)

class animation {
std::list< graphic > graphics;
public:

animation::animation( istream& is ) {
while ( is ) {
graphics.push_back( graphic( is ) );
}
}
};

Of course, animation and graphic should probably derive from some common
ABC as well...

Just some thoughts...
 
J

JoeC

Thanks much of all the help. I know there are better ways to write the
program. My goal is first make it work then move on to writing better
code. Now I got it working with an array I will experiment with an
vector or list to see if I can get that to work.
 

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
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top