473,699 Members | 2,752 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 3931

<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
2680
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
2319
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
3006
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
4225
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
2877
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
3729
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
20563
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
7857
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
8098
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
2734
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
8687
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
8617
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8884
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6534
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
5875
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
4376
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
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.