simplest way to write and read text file in c++

Here you can talk about C++ And C# And Other languages programming.

simplest way to write and read text file in c++

Postby BattleStar-Galactica » Sat Feb 03, 2007 8:05 am


weekend get bored

Code: Select all
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   string filename="test.txt";
   string line="hello world\r\n";
   fstream ofile(filename.c_str(),
      ios_base::out | ios_base::binary);
   if(!ofile.is_open())
    {
        cout << "Cannot Open " << filename.c_str()
             << " for writing\n";
        return 0;
    }
   for(int i=0; i<5; i++) //write 4 lines to file
      ofile.write(line.c_str(), line.length());
   ofile.close();
   return 0;
}


and read text file

Code: Select all
char FileBuffer[256];
   fstream ifile(filename.c_str(),
        ios_base::in | ios_base::binary);
   if(!ifile.is_open())
    {
        cout << "Cannot Open " << filename.c_str()
             << " for reading\n";
        return 0;
    }
   while(!ifile.eof())
   {

      ifile.getline(FileBuffer, 256);
      cout << FileBuffer << endl;
   }
   ifile.close();

seriously I dont use fstream in any of my programm, I use fopen,fread and fwrite. fstream for newbie to c++ :wink:
Big math problem
2 - 1 = 0 = without you I'm nothing
User avatar
BattleStar-Galactica
imFiles Master
imFiles Master
 
Posts: 565
Joined: Tue Sep 20, 2005 12:19 am
Location: safest place to hide

Postby Dody » Thu Feb 08, 2007 6:00 pm

I suggest that you catch the exceptions while writing and reading (it is a must in java now days :) )
and take a look about CFile at msdn -> http://msdn2.microsoft.com/en-us/library/60fh2b6f(VS.80).aspx
it is a MFC class

have fun :)
User avatar
Dody
imFiles Senior
imFiles Senior
 
Posts: 177
Joined: Tue Jan 04, 2005 2:44 pm

Postby BattleStar-Galactica » Thu Feb 08, 2007 9:04 pm

to make it simple the catch exception is avoid for the clarify
Last edited by BattleStar-Galactica on Thu Feb 08, 2007 9:09 pm, edited 1 time in total.
Big math problem
2 - 1 = 0 = without you I'm nothing
User avatar
BattleStar-Galactica
imFiles Master
imFiles Master
 
Posts: 565
Joined: Tue Sep 20, 2005 12:19 am
Location: safest place to hide

Postby NinjaCoder » Fri Feb 09, 2007 1:09 am

C++ is way above my little mind....im still a VB noob...lol :D
NinjaCoder
 

Postby BattleStar-Galactica » Fri Feb 09, 2007 1:59 am

Me too i'm vb6 noob
ninja if I were you, I learn c++ first; if you can code in c++ you can code in every programming language
Big math problem
2 - 1 = 0 = without you I'm nothing
User avatar
BattleStar-Galactica
imFiles Master
imFiles Master
 
Posts: 565
Joined: Tue Sep 20, 2005 12:19 am
Location: safest place to hide

Postby NinjaCoder » Fri Feb 09, 2007 2:02 am

thanks for that advice man.....that sounds real good :D
NinjaCoder
 


Return to C++, C# And Others Programming

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 0 guests