CSE 283 Introduction to Object Oriented Design

Barbara Nostrand, Ph.D.


 
 

Program to get a file name off the command line version 1

Prev | Next | Help Pages

// filename.cpp is a program to get a file name off the command line.
//
// Written by: John Koch
// Date:       6/2/99
// Purpose:    a program to get a file name off the command line
//             it then opens and reads the file line by line and
//             prints it to the screen.
//
// Specification:
// input(command line): filename, a string;
// output(screen): the file is output to the screen.
//******************************************************************

#include <iostream> // cin, cout, ... #include <fstream> // ifstream ... #include <string> // string ... using namespace std;

int main(int argc, char *argv[ ]) { ifstream infile; // define file infile string line; // string var to hold entire line if (argc < 2) { cout << "You forgot to put a file name on the command line."; cout << endl; } else { infile.open(argv[1]); // must open the file once if (!infile.is_open()) // error opening file cout << "Failed to open file " << argv[1] << endl; else { while (true) { getline(infile,line); // use file 'infile' as you would cin if (infile.eof()) break; cout << line << endl; } // end while true }

} // end outer else return 0; }

Prev | Next | Help Pages

 
  Home

FAQ

Help

Labs

Policies

Schedule

XEmacs

UNIX

C++ main 1

C++ main 2

C++ Strings

C++ Algorithms

C++ Vector

C++ Numbers

Package java.io

Package java.awt

Package javax.swing

Java GUI Tutorial




Last modified: 2007 SEP 09
bnostran@syr.edu