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

ifsteam

Why do I have to do the following if I want to open a file for reading
like
string filename = "data.txt";
ifstream ifs(filename.c_str());

I'd have thought that it would be quite elegant to do

ifstream ifs(filename);

instead!

--
http://www.munted.org.uk

Take a nap, it saves lives.
Apr 3 '06 #1
6 2677

"Alex Buell" <al********@munted.org.uk> wrote in message news:20060404001936.de426068.al********@munted.org .uk...
| Why do I have to do the following if I want to open a file for reading
| like
| string filename = "data.txt";
| ifstream ifs(filename.c_str());
|
| I'd have thought that it would be quite elegant to do
|
| ifstream ifs(filename);
|
| instead!
|

What is preventing you from doing so?

#include <fstream>
#include <string>

class IFStream
{
std::ifstream m_ifs;
public:
IFStream(std::string s_fn) : m_ifs(s_fn.c_str()) { }
~IFSTream() { }
...
};

int main()
{
std::string file("test.bin");
IFStream( file );
}
Apr 4 '06 #2

Alex Buell wrote in message
<20********************************@munted.org.uk> ...
Why do I have to do the following if I want to open a file for reading
like
string filename = "data.txt";
ifstream ifs(filename.c_str());

I'd have thought that it would be quite elegant to do

ifstream ifs(filename);

instead!


Because you defined 'string' wrong.

typedef char const * string;
string filename( "data.txt" );
std::ifstream ifs( filename );
if( not ifs ){
std::cerr<<"\nError opening file\n";
}

And, you don't 'have to do' it that way:

ifstream ifs( "data.txt" );

--
Bob <G> R
POVrookie
Apr 4 '06 #3
On Mon, 3 Apr 2006 21:57:25 -0400 "pj_hern" <pj*****@none.none> waved a
wand and this message magically appeared:
What is preventing you from doing so?

#include <fstream>
#include <string>

class IFStream
{
std::ifstream m_ifs;
public:
IFStream(std::string s_fn) : m_ifs(s_fn.c_str()) { }
~IFSTream() { }
...
};

int main()
{
std::string file("test.bin");
IFStream( file );
}


Oh yeah, good idea. But why use a c_str() with ifstream when it's so
simple to add an overloaded member function that takes a std::string?

--
http://www.munted.org.uk

Take a nap, it saves lives.
Apr 4 '06 #4
"Alex Buell" <al********@munted.org.uk> wrote in message
news:20060404092632.2c15d10d.al********@munted.org .uk...
On Mon, 3 Apr 2006 21:57:25 -0400 "pj_hern" <pj*****@none.none> waved a
wand and this message magically appeared:
What is preventing you from doing so?

#include <fstream>
#include <string>

class IFStream
{
std::ifstream m_ifs;
public:
IFStream(std::string s_fn) : m_ifs(s_fn.c_str()) { }
~IFSTream() { }
...
};

int main()
{
std::string file("test.bin");
IFStream( file );
}
Oh yeah, good idea. But why use a c_str() with ifstream when it's so
simple to add an overloaded member function that takes a std::string?


But isn't it just as simple to use .c_str() ?

--
http://www.munted.org.uk

Take a nap, it saves lives.

Apr 4 '06 #5
On Tue, 4 Apr 2006 03:33:15 -0700 "Jim Langston"
<ta*******@rocketmail.com> waved a wand and this message magically
appeared:
Oh yeah, good idea. But why use a c_str() with ifstream when it's so
simple to add an overloaded member function that takes a std::string?


But isn't it just as simple to use .c_str() ?


That's what I decided to do, in the end. It was the least ugly
solution.

--
http://www.munted.org.uk

Take a nap, it saves lives.
Apr 4 '06 #6
Alex Buell wrote:
Why do I have to do the following if I want to open a file for reading
like
string filename = "data.txt";
ifstream ifs(filename.c_str());

I'd have thought that it would be quite elegant to do

ifstream ifs(filename);

instead!


That's an artifact of the developing C++ standard, unfortunately, but
it will almost certainly be corrected in the next revision of the
standard (sometimes called C++0x).

Cheers! --M

Apr 4 '06 #7

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

Similar topics

13
by: puzzlecracker | last post by:
I would like to find out why exactly I wasn't able to open or execute the file.... how is it done in c++? ifsteam in("somefile"); if(!in) //why?
2
by: Pat | last post by:
Here is my problem that I am having with a current project. I went and backed up all my hard drives to DVD's. After backing up the drives, I used a batch file to get all the file names from the...
4
by: RafaƂ Maj Raf256 | last post by:
Hi, I wrote few filters working on streams of bytes, in example enciption, UTF-8 decoding and such. Now I wonder how can I turn them into classes derived from std::stream(?) or in other way to use...
3
by: amitpathak | last post by:
Hi I wan to know How we can open a file of.xls extension with the help of Visual C++ coding.
5
by: Chad | last post by:
say my input file is $ more suck ______ < gnu? > ------ \ , , \ /( )` \ \ \___ / | /- _ `-/ '
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.