Read From File In C++

by Shabbir on June 7, 2008

in C/C++

In the previous tutorial we explained how to do that in C#, now we will do that in C++.
So, create a Win32 C++ Console Application. This tutorial will be similar to the tutorial for writing to file in c++,
So, write this code

  1.  
  2. int main()
  3. {
  4. ifstream in;
  5. in.open("text.txt");
  6.  
  7. char line[256];
  8.  
  9. while(in.getline(line, 256))
  10. cout << line << endl;
  11.  
  12. in.close();
  13. }

Here we read the file line by line, and write its content to the console.
We have one char variable where we will store that lines, it will be long 256 characters. We are using the ifstream class, and creating an object from it, opening the file, do our work, and in the end we are closing the handle from our application to the file we read.
That's it. Very simple.

Sharing is Caring...

{ 1 comment… read it below or add one }

Unni April 19, 2010 at 2:33 pm

Tahnx i dont Want your making money online class(i have already subscribed it Can you sent me) C++ lessons

Reply

Leave a Comment

Previous post:

Next post:

    About the Author

  • author photo

    My Name is Shabbir Bhimani and I am developer by profession in the field of applications, web and database. Currently doing full time online marketing and like to share my experiences on how you can make money online @ CodeItWell.com. Read more ...


    See how you can get in touch with me.