Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8570

Re: reading a file into std::string

From red floyd <no.spam.here@its.invalid>
Newsgroups comp.lang.c++
Subject Re: reading a file into std::string
Date 2011-08-08 13:23 -0700
Organization A noiseless patient Spider
Message-ID <j1pgkl$q92$1@dont-email.me> (permalink)
References <4e3fe183$0$316$14726298@news.sunsite.dk> <cone.1312811905.617876.25291.500@monster.email-scan.com> <cone.1312812316.772351.25291.500@monster.email-scan.com>

Show all headers | View raw


On 8/8/2011 7:05 AM, Sam wrote:
> Sam writes:
>
>> arnuld writes:
>>
>>> I want to read a file into std::string. I am basically a C Programmer so
>>> it was quite hard for me to understand how to do it in C++. I did C++
>>> long time back (if you guys remember my name but I do remember Shiva and
>>> Victor Bazarov and others).
>>>
>>> I googled for it and this is the best what I could come up with. Do you
>>> guys have any suggestion for improvement ? and whether this is really a
>>> correct C++ program. (Compiled with "g++ -ansi -pedantic -Wall -Wextra")
>>
>> Your program is basically a C program. Here's a C++ program.
>>
>> #include <iostream>
>> #include <fstream>
>> #include <sstream>
>>
>> int main()
>> {
>> std::ostringstream o;
>>
>> o << std::ifstream("reference.cpp").rdbuf();
>>
>> std::string my_contents= o.str();
>>
>> return 0;
>> }
>
> … Or, here's a slightly more efficient version.
>
> #include <iostream>
> #include <fstream>
> #include <iterator>
> #include <algorithm>
>
> int main()
> {
> std::string my_contents;
>
> std::copy(std::istreambuf_iterator<char>(std::ifstream("reference.cpp").rdbuf()),
>
> std::istreambuf_iterator<char>(),
> std::back_insert_iterator<std::string>(my_contents));
>
> return 0;
> }
>
>


Avoid the copy.


#include <iostream>
#include <fstream>
#include <string>
#include <iterator>

int main()
{
    // note: extra parens on the constructor args to
    //   avoid potential "Most Vexing Parse" issues
    std::string my_contents(
       std::istreambuf_iterator<char>(
          (std::ifstream("reference.cpp").rdbuf())),
          (std::istreambuf_iterator<char>()));
}

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

reading a file into std::string arnuld <sunrise@invalid.address> - 2011-08-08 13:15 +0000
  Re: reading a file into std::string Marc <marc.glisse@gmail.com> - 2011-08-08 13:40 +0000
  Re: reading a file into std::string Sam <sam@email-scan.com> - 2011-08-08 08:58 -0500
    Re: reading a file into std::string Sam <sam@email-scan.com> - 2011-08-08 09:05 -0500
      Re: reading a file into std::string red floyd <no.spam.here@its.invalid> - 2011-08-08 13:23 -0700
        Re: reading a file into std::string arnuld <sunrise@invalid.address> - 2011-08-09 05:13 +0000
          Re: reading a file into std::string red floyd <no.spam.here@its.invalid> - 2011-08-08 23:12 -0700
            Re: reading a file into std::string arnuld <sunrise@invalid.address> - 2011-08-09 06:45 +0000
              Re: reading a file into std::string red floyd <no.spam.here@its.invalid> - 2011-08-09 09:26 -0700
                Re: reading a file into std::string arnuld <sunrise@invalid.address> - 2011-08-10 10:58 +0000
                Re: reading a file into std::string "Bo Persson" <bop@gmb.dk> - 2011-08-10 16:19 +0200
                Re: reading a file into std::string red floyd <no.spam.here@its.invalid> - 2011-08-10 10:06 -0700
              Re: reading a file into std::string red floyd <no.spam.here@its.invalid> - 2011-08-09 09:28 -0700
              Re: reading a file into std::string red floyd <no.spam.here@its.invalid> - 2011-08-09 09:33 -0700
                Re: reading a file into std::string arnuld <sunrise@invalid.address> - 2011-08-10 12:13 +0000
                Re: reading a file into std::string Nobody <nobody@nowhere.com> - 2011-08-11 08:57 +0100
                Re: reading a file into std::string "Bo Persson" <bop@gmb.dk> - 2011-08-11 12:51 +0200
                Re: reading a file into std::string Nobody <nobody@nowhere.com> - 2011-08-12 05:00 +0100
      Re: reading a file into std::string Nana J. Osei <mppccf0987@att.net> - 2011-08-13 23:18 +0000
        Re: reading a file into std::string Sam <sam@email-scan.com> - 2011-08-13 20:58 -0500
    Re: reading a file into std::string Juha Nieminen <nospam@thanks.invalid> - 2011-08-09 11:26 +0000
      Re: reading a file into std::string Marc <marc.glisse@gmail.com> - 2011-08-09 12:54 +0000
        Re: reading a file into std::string Juha Nieminen <nospam@thanks.invalid> - 2011-08-09 16:33 +0000
          Re: reading a file into std::string Marc <marc.glisse@gmail.com> - 2011-08-09 16:47 +0000
            Re: reading a file into std::string red floyd <no.spam.here@its.invalid> - 2011-08-09 11:27 -0700
              Re: reading a file into std::string Joshua Maurice <joshuamaurice@gmail.com> - 2011-08-11 15:34 -0700
                Re: reading a file into std::string Juha Nieminen <nospam@thanks.invalid> - 2011-08-12 05:47 +0000
      Re: reading a file into std::string arnuld <sunrise@invalid.address> - 2011-08-09 12:57 +0000
        Re: reading a file into std::string Joshua Maurice <joshuamaurice@gmail.com> - 2011-08-11 15:40 -0700
  Re: reading a file into std::string Jean-Luc Romano <jl_post@hotmail.com> - 2011-08-12 11:28 -0700
    Re: reading a file into std::string Lynn McGuire <lmc@winsim.com> - 2011-08-12 15:56 -0500

csiph-web