Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reading a file into std::string

Reply
Thread Tools

reading a file into std::string

 
 
arnuld
Guest
Posts: n/a
 
      08-08-2011
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")



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


int main()
{
std::string my_contents, tmp_contents;
std::ifstream my_file("reference.cpp");
if(!my_file)
{
std::cerr << "Error Opening file" << std::endl;
exit(EXIT_FAILURE);
}

while(my_file)
{
std::getline(my_file, tmp_contents);
my_contents += tmp_contents;
my_contents += "\n";
}

std::cout << "String contents are: "<< "\n"
<< my_contents << std::endl;

my_file.close();

return 0;
}





-- arnuld
www.LispMachine.Wordpress.com
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      08-08-2011
arnuld wrote:

> 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).

[...]
> std::string my_contents, tmp_contents;
> std::ifstream my_file("reference.cpp");
> if(!my_file)
> {
> std::cerr << "Error Opening file" << std::endl;
> exit(EXIT_FAILURE);
> }
>
> while(my_file)
> {
> std::getline(my_file, tmp_contents);
> my_contents += tmp_contents;
> my_contents += "\n";
> }


You might as well use std::getline(my_file, my_contents, '\0'),
assuming there is no null character in your file. Not that it is a
good solution, but at least you don't have a loop.
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-08-2011
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:stringstream 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::ifst ream("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>()));
}
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-09-2011
> On Mon, 08 Aug 2011 13:23:49 -0700, red floyd wrote:

> 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>()));
> }



Can you please tell me in brief how it works. 2nd, you said "Avoid the
copy", so your program does not copy ?

All I can see is its a template. What about error checking whether file
was opened successfully or not, we are nto even closing the file after we
finish ? I have Stroustrup 3/e in my hands, checking out what rdbuf()
does from section 21.6.3, page 644.


--
-- arnuld
www.LispMachine.Wordpress.com
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      08-09-2011
On 8/8/2011 10:13 PM, arnuld wrote:
>> On Mon, 08 Aug 2011 13:23:49 -0700, red floyd wrote:

>
>> 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>()));
>> }

>
>
> Can you please tell me in brief how it works. 2nd, you said "Avoid the
> copy", so your program does not copy ?


The example I was referring to had constructed an empty string, and then
copied the file into it.

My example creates the string with the contents of the file, by using
a constructor that takes two iterators.

In actual practice, there won't be much difference, but one-upmanship
sometimes comes into play on this newsgroup! <grin>

> All I can see is its a template. What about error checking whether file
> was opened successfully or not, we are nto even closing the file after we
> finish ? I have Stroustrup 3/e in my hands, checking out what rdbuf()
> does from section 21.6.3, page 644.


It is "template" code. Error checking, etc... is left as an exercise
for the reader.

rdbuf() returns the streambuf underlying the fstream.



 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-09-2011
> On Mon, 08 Aug 2011 23:12:30 -0700, red floyd wrote:

> My example creates the string with the contents of the file, by using a
> constructor that takes two iterators.


Chapter Strings, Section 20.3.4 I don't see any constructir which takes 2
arguments. If I go to section 16.3.4 then it does have two iterators in a
constructor but that is std::vector not std::string


> It is "template" code. Error checking, etc... is left as an exercise
> for the reader.


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.



--
-- arnuld
www.LispMachine.Wordpress.com
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      08-09-2011
Sam <(E-Mail Removed)> wrote:
> Your program is basically a C program. Here's a C++ program.


I think the std::getline() with a null line terminator is a much simpler
and better solution.
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      08-09-2011
Juha Nieminen wrote:

> Sam <(E-Mail Removed)> wrote:
>> Your program is basically a C program. Here's a C++ program.

>
> I think the std::getline() with a null line terminator is a much simpler
> and better solution.


ostream:perator<<(streambuf*) has the advantage that it doesn't need
to check characters one by one. Of all the solutions in the thread, it
looks like the one most likely to perform batch copies.

(of course anything C++ will be much slower than a version with mmap)
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-09-2011
> On Tue, 09 Aug 2011 11:26:29 +0000, Juha Nieminen wrote:

> I think the std::getline() with a null line terminator is a much
> simpler and better solution.


Stroustrup, section 20.3.15, page 598

The getline() function reads a line terminated by eol into its
string, expanding the string as needed to hold the line. If no eol
argument is provided, a newline '\n' is used as the delimiter. The line
terminator is removed from the stream but not entered into string.

where string = 2nd argument, eol = 3rd argument

now my point is when you give '\0' (its called null or NULL ?) as 3rd
argument which is not present in the input stream, what will be its
behavior ?

(1) will getline() keep on looking for it till its reaches EOF (End of
File) and read whole file into the string

(2) If I give anything which is not present in the input e.g. '#' will it
behave the same ?




--
-- arnuld
www.LispMachine.Wordpress.com
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      08-09-2011
On 8/8/2011 11:45 PM, arnuld wrote:
>> On Mon, 08 Aug 2011 23:12:30 -0700, red floyd wrote:

>
>> My example creates the string with the contents of the file, by using a
>> constructor that takes two iterators.

>
> Chapter Strings, Section 20.3.4 I don't see any constructir which takes 2
> arguments. If I go to section 16.3.4 then it does have two iterators in a
> constructor but that is std::vector not std::string
>


ISO/IEC 14882:2003, sections 21.3/6 [lib.basic.string] and
21.3.1/14 [lib.string.cons]

template<class InputIterator>
basic_string(InputIterator begin, InputIterator end,
const Allocator& a = Allocator());

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading whole file into memory. Parsing 'C' like file efficently n_macpherson@sky.com Perl Misc 6 06-17-2008 10:26 PM
converting a text file into an "insert into ..." file kublaikhan55@hotmail.com Ruby 5 07-23-2006 07:35 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM
Reading Blank Lines into StringTokens into Vector Array Row Java 0 04-14-2004 07:12 AM



Advertisments