Saturday, August 7, 2010

Reading entire file in one go.

Solution 1:
std::ifstream in("circle.cc");
std::istreambuf_iterator < char > begin(in), end;
std::string str(begin, end);

Solution 2:
std::ifstream input("circle.cc");
std::ostringstream temp;
temp << input.rdbuf();
std::string str = temp.str();

In both the cases,
std::cout << str;
will print out entire file.

Perl solution:
open CIRCLE, "circle.cc";
@mylist = ;
close CIRCLE;

C++ and Perl both take 3 lines of code.
Not bad for a system programming language!!

No comments:

Post a Comment