Faqts : Computers : Programming : Languages : C++

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 12 people (58%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

Does anybody know how to open a text file and evaluate one line at a time?

Mar 18th, 2004 18:36
Aaron Hawley, Chris Ollar, http://www.research.att.com/~bs/bs_faq2.html#read-string


#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char **argv)
{
  if (argc != 2)
    {
      cout << "You need to specify one file" << endl;
    }
  else
    {
      char *filename = argv[1];
      cout << "Your file: " << filename << endl;
      ifstream infile(filename);
      string one_line;
      while(getline(infile, one_line)) {
	cout << "Your line: " << one_line << endl;
      }
    }
}