473,395 Members | 2,006 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,395 software developers and data experts.

How do i copy an entire file into a string

I'm trying to make a subprogram,
Code:

void file2String(istream& in, string& theFile)
that can read an entire file into a string variable, including line
breaks and i have no idea what im doing. I'm using the GNU g++
compiler on Fedora 4 Linux Distribution.
Thanks in advance for the help

Oct 11 '06 #1
4 4276
owolablo wrote:
I'm trying to make a subprogram,
Code:

void file2String(istream& in, string& theFile)
that can read an entire file into a string variable, including line
breaks and i have no idea what im doing.
Use 'read' member of 'istream' and keep appending what you read to
the string, until an error or end-of-file.
I'm using the GNU g++
compiler on Fedora 4 Linux Distribution.
We don't care.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #2
On Wed, 11 Oct 2006 08:59:48 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>Use 'read' member of 'istream' and keep appending what you read to
the string, until an error or end-of-file.
You can also reserve enough space to read the entire file with one
dynamcic allocation (but keep std::string as out param and check the
return values):
http://www.cplusplus.com/ref/iostrea...eam/tellg.html

Best wishes,
Roland Pibinger
Oct 11 '06 #3
owolablo wrote:
I'm trying to make a subprogram,
Code:

void file2String(istream& in, string& theFile)
that can read an entire file into a string variable, including line
breaks and i have no idea what im doing.
Try this:

#include <iterator>

void file2string ( std::istream & in, std::string & str ) {
std::string dummy ( std::istreambuf_iterator<char>( in ),
(std::istreambuf_iterator<char>()) );
str.swap( dummy );
}

Best

Kai-Uwe Bux
Oct 11 '06 #4
"owolablo" <ow********@yahoo.comwrites:
void file2String(istream& in, string& theFile)

that can read an entire file into a string variable, including line
breaks and i have no idea what im doing. I'm using the GNU g++
compiler on Fedora 4 Linux Distribution.
Thanks in advance for the help
in << ifstream(theFile).rdbuf();

To read it into a string make `in' be a stringstream.

--
Gruß, Jens
Oct 11 '06 #5

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

Similar topics

4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
1
by: Jeroen Pot | last post by:
Hi, I have a dataset "Mydataset" which contains of a few fields. (lets say dept_id and dept_name) A user can select several of these, and i want to export this to an access database. I have...
5
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
2
by: Xavier Valdés | last post by:
Hi all, I would like to copy FILES from the clipboard to a desired folder from VB.NET. I was able to copy files (with filedrop data format) to the clipboard but I don't know how to catch this...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
1
by: Talal Itani | last post by:
Hello, I need the program I am writing to duplicate a folder, from one drive to another drive. I need a progress report, and write-verify. What is a good way to do that? I am using C#...
4
by: GS | last post by:
from the online help, looks like to code step by step to copy content of directory to another whereas I can use online line code for directory move or delete. I am using 2005 express .net 2 I did...
4
by: jodleren | last post by:
While reading php.net I found " bool copy ( string $source , string $dest ) Makes a copy of the file source to dest . If you wish to move a file, use the rename() function. " ..... what?...
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: 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
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...
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
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...
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...

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.