473,748 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

istream and string

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

Apr 20 '06 #1
4 3932

<nn*******@gmai l.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.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(if s, line);
std::cout << "string contains: " << line << '\n';
return 0;
}

-Mike
Apr 20 '06 #2
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

nn*******@gmail .com 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


Apr 20 '06 #3
ss*******@gmail .com 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
Apr 21 '06 #4
ss*******@gmail .com 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::stringstre am;
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
Apr 22 '06 #5

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

Similar topics

0
2682
by: Juan Carlos CORUÑA | last post by:
Hello all, I have developed an automation server using Mark's win32 extensions and I must return a IStream COM object from one method of the automation server. Here is an extract of my code: class Stream: _public_methods_ =
3
2323
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've gotten everything working that I want working so far, except for std::istream& operator>>(std::istream&, Str&) The way my Str class manages itself of course requires that the size of the char array to store is known when it is allocated. The...
6
3009
by: Jason K | last post by:
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a function to count lines in a large file that start with a particular pattern. The file can contain large amounts of non-text crap, which may make some lines very long (so using getline() with a std::string isn't feasable). So I dug around looking...
3
4227
by: Charles Law | last post by:
Could anyone please tell me what is wrong with the code below: STDMETHODIMP CSimpleObj::WriteUnmanagedData(BSTR pString, IStream** ppData) { HRESULT hr; hr = CreateStreamOnHGlobal(0, TRUE, ppData); if (FAILED(hr)) return hr;
13
2882
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } istream& operator >>( istream& input, String^ str )
13
3732
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of these methods EXTRACT the data at one point or another. The other problem is there appears to be NO WAY to get at the actual buffer pointer (char*) of the characters in the stream. There is a way to get the streambuf object associated with the...
7
20586
by: John Salmon | last post by:
I'm working with two libraries, one written in old school C, that returns a very large chunk of data in the form of a C-style, NUL-terminated string. The other written in a more modern C++ is a parser for the chunk of bytes returned by the first. It expects a reference to a std::istream as its argument.
2
7867
by: david.crow | last post by:
Given the following input file: bob 1 2 3 4 5 mary 2 3 4 5 6 7 susan 3 4 5 6 7 8 9 This code snippet does not read it correctly: class Student {
4
8103
by: Ralf | last post by:
Hallo, I'm trying to call a COM function from C#. The function has a parameter from the type array of IStream* (IStream**). The COM component (out-proc-server (.exe)) has the following code: (C++) STDMETHODIMP CIntf::SendFiles(int noOfStreams, IUnknown** dataStreams) {
0
2741
by: admb600 | last post by:
I wouldn't normally beg but my dissertation is due in, in a couple of weeks and I am in way over my head here.. Fixing this issue will make or break the project and my degree.. The goal is to extract the raw html from Internet Explorer using a BHO. When accessing the HTML via the mshtml.HTMLDocument (DOM) object you get a version of the HTML that is different from what you see when you "right click --> view source". IE changes the code for...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.