Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > istream and string

Reply
Thread Tools

istream and string

 
 
nnandini8@gmail.com
Guest
Posts: n/a
 
      04-20-2006
Hello guys,
I have a small doubt in istream.
I have a file calles infile.txt
infile.txt contains Hello World
now I wanted to add these contents to a string.
ie i need to have hello world in a string
Note there is a space between Hello and WOrld

Any help will be greatly appreciated
Thank You
Nandini

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      04-20-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello guys,
> I have a small doubt in istream.
> I have a file calles infile.txt
> infile.txt contains Hello World
> now I wanted to add these contents to a string.
> ie i need to have hello world in a string
> Note there is a space between Hello and WOrld
>
> Any help will be greatly appreciated


// (error checking omitted)
#include <fstream>
#include <iostream>
#include <string>

int main()
{
std::ifstream ifs("infile.txt");
std::getline(ifs, line);
std::cout << "string contains: " << line << '\n';
return 0;
}

-Mike


 
Reply With Quote
 
 
 
 
sshakir56@gmail.com
Guest
Posts: n/a
 
      04-20-2006
Thanks for the reply Mike.
It worked but what do I need to do if I have like
hello
world
in 2 seperate lines
Thanks in advance

Nandini

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello guys,
> I have a small doubt in istream.
> I have a file calles infile.txt
> infile.txt contains Hello World
> now I wanted to add these contents to a string.
> ie i need to have hello world in a string
> Note there is a space between Hello and WOrld
>
> Any help will be greatly appreciated
> Thank You
> Nandini


 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      04-21-2006
(E-Mail Removed) wrote:
> Thanks for the reply Mike.
> It worked but what do I need to do if I have like
> hello
> world
> in 2 seperate lines
> Thanks in advance


Use another getline to get the next line.

Regards,
Ben
 
Reply With Quote
 
=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
Guest
Posts: n/a
 
      04-22-2006
(E-Mail Removed) wrote:

> Thanks for the reply Mike.
> It worked but what do I need to do if I have like
> hello
> world
> in 2 seperate lines


Here's an alternative solution:

#include <fstream>
#include <sstream>
#include <string>
#include <iostream>

int main()
{
using std::ifstream;
using std::stringstream;
using std::string;
using std::cout;

ifstream file("Test.txt");
stringstream buffer;
buffer << file.rdbuf();
string contents = buffer.str();

cout << "Contents: " << contents << '\n';
}

Regards,

--
Ney André de Mello Zunino
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does the C++ standard define the global function of " istream&operator >>(istream& in, string& str); "? xmllmx C++ 5 06-15-2010 11:57 AM
[iostream] how to get the rest string of a istream mos C++ 3 06-26-2006 03:29 AM
Setting mininum string read width in istream Richard Clay C++ 2 04-05-2005 05:50 PM
from std::string to std::istream? Gernot Frisch C++ 4 03-18-2005 02:26 PM
istream >> (my own string class) - low-level solution? matthurne C++ 3 07-22-2004 02:37 PM



Advertisments