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


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

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-09 09:33 -0700
Organization A noiseless patient Spider
Message-ID <j1rnh3$fo$1@dont-email.me> (permalink)
References (2 earlier) <cone.1312812316.772351.25291.500@monster.email-scan.com> <j1pgkl$q92$1@dont-email.me> <4e40c1f8$0$310$14726298@news.sunsite.dk> <j1qj58$ue4$1@dont-email.me> <4e40d7a6$0$310$14726298@news.sunsite.dk>

Show all headers | View raw


On 8/8/2011 11:45 PM, arnuld wrote:

> you mean this code will be real-life code based on the ideas you have
> given me:
>
> #include<iostream>
> #include<fstream>
> #include<string>
>
>
> int main()
> {
>    std::ifstream my_file("reference.cpp");
>    if(!my_file)
>      {
>        std::cerr<<  "Error Opening file"<<  std::endl;
>        exit(EXIT_FAILURE);
>      }
>
>    std::string my_contents(std::istreambuf_iterator<char>(my_file.rdbuf()),
> 			  (std::istreambuf_iterator<char>()));
>
>    std::cout<<  "String contents are:"<<  "\n"
> 	<<  my_contents<<  std::endl;
>
>    my_file.close();
>
>    return 0;
> }
>
> as usual compiled with "gcc -ansi -pedantic -Wall -Wextra" and it
> compiles and runs fine.
Technically. you need to #include <cstdlib> to define EXIT_FAILURE.

In actual practice, obviously one of <iostream>, <fstream>, or <string>
is including it.

Also, I believe std::endl is defined in <ostream>.  In C++11 <iostream> 
covers that, but in C++03, <iostream> does not officially include
<ostream>, though as far as I know, all known C++ compilers do the 
nested include.




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