C + +: Read all the contents of a text file (TXT) into a string, the most efficient method

Source: Internet
Author: User

Code:

* * * * main.cpp * *  Created on:2014 June 17 
 *      author:spike/
      
/*eclipse CDT, gcc 4.8.1* /
      
#include <iostream>  
#include <fstream>  
#include <string>  
#include < Cerrno>  
      
std::string get_file_contents (const char *filename)  
{  
  std::ifstream in (filename, Std::ios :: In | std::ios::binary);  
  if (in)  
  {  
    std::string contents;  
    IN.SEEKG (0, std::ios::end);  
    Contents.resize (In.tellg ());  
    IN.SEEKG (0, Std::ios::beg);  
    In.read (&contents[0], contents.size ());  
    In.close ();  
    return (contents);  
  }  
  throw (errno);  
}  
      
int main (void)  
{  
    std::cout << get_file_contents ("The Rabbit of the River") << Std::endl;  
    return 0;  
}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Output:

The rabbit is famous for its cowardice, and often the shock is always like a stone on their heart.  
      
once, a large number of rabbits gathered together for their own timid incompetence and sad, lament their life is full of danger and fear. The  
      
more they talk, the more sad they are, as if there have been many unfortunate occurrences in themselves, and that is why they became rabbits.  
      
at this point, the negative imagination emerges endlessly.  
      
they lament their natural misfortune, there is no strength and wings, no teeth, the day can only be in the east afraid of the west to spend,  
      
even want to abandon all big sleep, also have anything can hear the long ear obstruction, redness of the eyes will become more red.  
      
they feel that their lives are meaningless, and this is the source of their self-loathing. They all felt that, rather than a lifetime of fear, not as good as a dead.  
      
as a result, they decided to jump off the cliff to end their own lives, ending all the trouble. So decided, so they go to the cliff together, want to drowned himself.  
      
at this time, some frogs are surrounded by the lake squatting, heard the rapid footsteps, formidable enemy, immediately jumped into the deep water to escape.  
      
This is the rabbit every time to the pond will see the scene, but today, a rabbit suddenly understand what,  
      
it loudly said: "Quickly stop, we do not have to scare to death to find life, because we can see, there are more cowardly animals than us!" "  
      
So, the rabbit's mood wonderfully suddenly enlightened up, as if a courage poured out, so they go home with great joy."  
      
great principles: Do not complain about the injustice of fate for our present experience, in fact, there are many more unfortunate people in the world,  
      
think that those who are more unfortunate still live strong, why can't we?

Author: csdn Blog spike_king

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.