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

Want to pass a file name to a function

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?

Mar 12 '06 #1
6 7497

"JoeC" <en*****@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
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());
Mar 12 '06 #2
"JoeC" <en*****@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
: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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Mar 12 '06 #3

"JoeC" <en*****@yahoo.com> schreef
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/libstd..._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::openmode __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
Mar 12 '06 #4
In article <11**********************@v46g2000cwv.googlegroups .com>,
"JoeC" <en*****@yahoo.com> wrote:
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...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 12 '06 #5
Thanks so much that worked.

Mar 12 '06 #6
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.

Mar 13 '06 #7

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

Similar topics

15
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global...
2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
13
by: Maroon | last post by:
Hi, I want to write a standard java user defined function, like this example:- select db_fun("roll"), roll from student; **db_fun() will work for all tables of the all databse here db_fun is...
0
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
1
by: colleen1980 | last post by:
Hi: Can any one please tell me that how to i pass the two textbox values in the new page. If i use the form action in the popup window page then the new page is open in the same popup window as i...
5
by: gdarian216 | last post by:
can I pass grades.projects in my function call that is void get_scores(ifstream& infile, int num_scores, grades.projects) and the function would look like void get_scores(ifstream& infile, int...
1
by: Rocky86 | last post by:
hi ppl bascially I am require to pass my php coding to actionscript and the actionscript will seperate the code of my php how do I do it? Must I create a function for php to be pass into actionscript...
7
by: dries | last post by:
Hello lads, I'd like to do the following in C: 1. The user writes a function with predefined arguments and return value in a separate file. 2. The program is compiled as my written 'main'...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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?

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.