The Way of C++ Problem Solving

August 9, 2011 at 11:36 am | Posted in C++, Hacking | Leave a comment

Many programmers write their programs in C and then save the program with extension .cpp and call it C++ program. I am one of them, except that I don’t call those programs C++ programs. I don’t know have much knowledge of C++, mostly work in C. Over time I have read so many articles and blogposts that I am fully convinced beyond any doubt that C++ and C are two similar looking and very different languages. They were designed with different aims, they have entirely different philosophies about designing a program. Unlike what most programmers say that C anc C++ are brothers, I will say they are not even far distant cousins. C and C++ are 2 persons who belong to different nationality and have different skin color and they speak different languages. To know the true way of C++ I thought of writing a C++ program which reads a file and saves that into a std::string (This is first lesson, use std:: string instead of using namespace std which defeats the aim of namespaces. I posted the discussion my probblem on comp.lang.c++ which you can read on archives like rhinocerus and velocityreviews:

http://www.velocityreviews.com/forums/t752581-reading-a-file-into-std-string.html

http://www.rhinocerus.net/forum/language-c/682219-reading-file-into-std-string.html

I wrote the code as a C Programmer who did some google search and read a few pages of Stroustrup. Red Floyd posted a true C++ version. You see how much the difference in thinking, Red Floyd’s code is much superior than mine. You can easily conclude that C++ what you know or what your colleagues know is not really true C++. This program made me think harder about my learning and experience as a computer programmer. Solving problems in C++ is really vastly different from the way we do it in C. Don’t write C code and save the file with extension .cpp. If you want to write C code then please save the file as .c Lessons learned

#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;
}

 

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

  /* This was posted by "redfloyd" on comp.lang.c++ */
  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;
  return 0;
}

 


Copyright © 2011 Arnuld Uttre, Village – Patti, P.O – Manakpur, Tehsil – Nangal, Distt. – Ropar, Punjab (INDIA)

Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice, and the copyright notice, are preserved.

Advertisements

Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: