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

std::string from file input

Hello,
I am trying to read a big file into a string. AFAIK std::string can read
in words or "lines" from a stream. So one option is to continuously
append all the "lines" from the file to the string.

The second option is to use the istream::read(Ch *p,streamsize n)
function and then create a std::string from 'buf'. However this is a
waste of time and space because (if I am not mistaken) the std::string
constructor will make a copy of the character array pointed to by buf.

Does anyone know any other (fast) way of doing this?

Thanks in advance,
Alexandros Frantzis
Jul 22 '05 #1
3 3079

"Alexandros Frantzis" <al*@alf.alf> wrote in message
news:bv**********@nic.grnet.gr...
Hello,
I am trying to read a big file into a string. AFAIK std::string can read
in words or "lines" from a stream. So one option is to continuously
append all the "lines" from the file to the string.

The second option is to use the istream::read(Ch *p,streamsize n)
function and then create a std::string from 'buf'. However this is a
waste of time and space because (if I am not mistaken) the std::string
constructor will make a copy of the character array pointed to by buf.

Does anyone know any other (fast) way of doing this?

Thanks in advance,
Alexandros Frantzis


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

int main()
{
std::string filename("myfile");

std::ifstream ifs(filename.c_str());
if(!ifs)
{
std::cerr << "Cannot open file '" << filename << "'\n";
return EXIT_FAILURE;
}

std::ostringstream oss;
oss << ifs.rdbuf();

if(!ifs && !ifs.eof())
std::cerr << "Error reading file '" << filename << "'\n";

std::string contents(oss.str());
std::cout << "file_contents:\n" << contents << '\n';

return 0;
}
-Mike
Jul 22 '05 #2
Alexandros Frantzis wrote:
Hello,
I am trying to read a big file into a string. AFAIK std::string can read
in words or "lines" from a stream. So one option is to continuously
append all the "lines" from the file to the string.

The second option is to use the istream::read(Ch *p,streamsize n)
function and then create a std::string from 'buf'. However this is a
waste of time and space because (if I am not mistaken) the std::string
constructor will make a copy of the character array pointed to by buf.

Does anyone know any other (fast) way of doing this?

Thanks in advance,
Alexandros Frantzis


I would never read an entire file into a string, since
files are notorisly bigger than a computers memory.

One method is to use a std::vector and ifstream::read().
The std::vector will expand as needed. There are ways to
optimize this by reserving extra space.

Is there a need to read the entire file into memory?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3
In article <bv**********@nic.grnet.gr>, al*@alf.alf says...
Hello,
I am trying to read a big file into a string. AFAIK std::string can read
in words or "lines" from a stream. So one option is to continuously
append all the "lines" from the file to the string.

The second option is to use the istream::read(Ch *p,streamsize n)
function and then create a std::string from 'buf'. However this is a
waste of time and space because (if I am not mistaken) the std::string
constructor will make a copy of the character array pointed to by buf.


The usual way is to read from the stream's buffer into a stringstream:

std::ostringstream buffer;
buffer << input.rdbuf();

at this point, buffer.str() is an std::string containing the entire
contents of the file.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #4

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

Similar topics

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(...
4
by: Christopher | last post by:
I am using std::string to parse a command given by the user, I don't understand why the following snippet is not working as expected. string buffer, // store commands from...
1
by: James Vanns | last post by:
I want to be able to print out (and read in) characters with accents (for example French and Italian text). So far I have this: std::locale lang (getenv ("LANG")); which seems to set the...
12
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
2
by: Starfox | last post by:
The Following Code: //Setup the options string InputName, OutputSkeletonName, OutputModelName; program_options::options_description OptionDesc("Available Options"); OptionDesc.add_options()...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
11
by: Peter Olcott | last post by:
Does C++ have anything like this?
13
by: Creativ | last post by:
I've looked through this thread and still have quetions. Suppose In visual studio 2005, I write the following #pragam managed class ManagedWrapper { void CallUnmanagedMethod() // The unmanaged...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
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...

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.