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

I want to create a std::ifstream using std::string


Hi, all.

Is there any way to create an instance of std::ifstream using
std::string.
(through std::ifstream's constructor or assignment operator or
iterator, etc...)

i.e.
std::string str = "this is a string";
std::ifstream ifs = str; //<----- this is a just pseudo code.

(^^;; I can solve this using making temporty file and write the str on
the file and reading it using std::ifstream...
but I don't want to make a additional file io ^^)

plz show me the way~

Apr 27 '07 #1
5 12308
* Assertor:
>
Is there any way to create an instance of std::ifstream using
std::string.
(through std::ifstream's constructor or assignment operator or
iterator, etc...)

i.e.
std::string str = "this is a string";
std::ifstream ifs = str; //<----- this is a just pseudo code.

(^^;; I can solve this using making temporty file and write the str on
the file and reading it using std::ifstream...
but I don't want to make a additional file io ^^)
Why don't you use a std::istringstream.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 27 '07 #2
On Apr 27, 7:28 pm, Assertor <gidae...@gmail.comwrote:
Hi, all.
how about this follow?

std::string str = "this is a string";
std::ifstream ifs(str.c_str());

Apr 27 '07 #3
wh****@gmail.com wrote:
On Apr 27, 7:28 pm, Assertor <gidae...@gmail.comwrote:
>Hi, all.
how about this follow?

std::string str = "this is a string";
std::ifstream ifs(str.c_str());
this is a bad error, because in this case str is taken as a filename.

Regards,

Zeppe
Apr 27 '07 #4
On Apr 27, 1:28 pm, Assertor <gidae...@gmail.comwrote:
Is there any way to create an instance of std::ifstream using
std::string.
(through std::ifstream's constructor or assignment operator or
iterator, etc...)
i.e.
std::string str = "this is a string";
std::ifstream ifs = str; //<----- this is a just pseudo code.
(^^;; I can solve this using making temporty file and write the str on
the file and reading it using std::ifstream...
but I don't want to make a additional file io ^^)
Why do you want to do this? Normally, except for creation, you
should be using the istream interface (from the base class).
And of course, this works just as well with an istringstream as
with an ifstream.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 27 '07 #5

Zeppe wrote in message...
wh****@gmail.com wrote:
On Apr 27, 7:28 pm, Assertor <gidae...@gmail.comwrote:
Hi, all.
how about this follow?

std::string str = "this is a string";
std::ifstream ifs(str.c_str());

this is a bad error, because in this case str is taken as a filename.
Regards,
Zeppe
{
std::string str = "\"this is a string\"";
std::ifstream ifs( str.c_str() );
ifs << str << std::endl;
}

There! All fixed.
<G>

note: On some systems you need to put the filename in quotes if there are
spaces in the filename.
--
Bob R
POVrookie
Apr 28 '07 #6

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

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
3
by: Mathieu Malaterre | last post by:
Hello, I have currently a piece a code which look like this: foo.h ------------------- extern const std::string NotFound; foo.cxx
9
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Angus | last post by:
I am working with a C API which often requires a char* or char buffer. If a C function returns a char* I can't use string? Or can I? I realise I can pass a char* using c_str() but what about...
4
by: Chris Forone | last post by:
hello group, why have i to bracket the second ctor param in the following example? thx & hand, chris #include <fstream> #include <iterator> int main()
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
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...

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.