20-CS-122-001 Computer Science II Spring 2012
Read From File - One Pass

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures      Source

#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;

// Simple use - read all file tokens and print them to console
int main(int argc, char **argv) {
   char buffer[1024];
   if (argc != 2) { 
      cout << "\nUsage: " << argv[0] << " \n";
      exit(0);  
   }

   fstream *fin = new fstream(argv[1], ios::in);
   if (fin->fail()) {
      cerr << "Cannot open file " << argv[1] << "\n";
      exit(0);
   }
   while (*fin >> buffer) cout << "[" << buffer << "]\n";
   fin->close();
}