ex8-13.cpp - Example 8-13 File I/O - A DOS grep command

// File: ex8-13.cpp - A grep command for DOS

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

// The macro __GNUG__ is set for GNU C++
#ifdef __GNUG__
#include <unistd.h>		// for access()
#else
#include <io.h>         // for _access()
#endif

int main(int argc, char* argv[])
{
	char		filename[64], 
				buffer[1024],
				command[128],
				tempFilename[9];
	int			lineno,	
				hits = 0, 
				files = 0,
				system_command_status;
	ifstream	fin1, 
				fin2;

	// Check command-line syntax
	if (argc != 3) {
		cerr << "Syntax error\ngrep [target text] [target file(s)]\n";
		exit (-1);
	}
	
	// create a temporary file to hold the filenames to be searched
	strcpy(tempFilename,"tempa");		// first possible filename
	
	// The access() or _access() functions are used to status the existence of a file
	
	// If you're using a GNU C++ compiler, use access()
#ifdef __GNUG__
	while (access(tempFilename,0) == 0 && tempFilename[4] <= 'z')
	// If you're not using a GNU C++ compiler, use _access()
#else
	while (_access(tempFilename,0) == 0 && tempFilename[4] <= 'z')
#endif
		tempFilename[4]++;

	// If you've tried all filenames from "tempa" to "tempz", then give up 
	if (tempFilename[4] > 'z') {
		cerr << "Unable to create a temp* file.  Please cleanup\n";
		exit (-2);
	}

	// Create the command:  "dir /b [filename(s)] > [tempfilename]
	strcpy(command,"dir /b ");
	strcat(command,argv[2]);
	strcat(command," > ");
	strcat(command,tempFilename);

	// system() allows you to issue operating system commands
	system_command_status = system(command);

	// check the status of the system command
	if (system_command_status == -1) {
		cerr << "Error with system command: " << command << endl;
		exit(-3);
	}
	
	// open temporary file (containing names of files to be searched)
	fin1.open(tempFilename);
	
	// Make sure the tempFilename is not empty
	if (fin1.peek() == EOF) {
		cerr << "Error: target file(s) do(es) not exist, dummy\n";
		exit (-4);
	}
	
	// for each file in tempFilename, search for the target string
	while (fin1.getline(filename,sizeof(filename))) {

		// open the next file to be searched
		fin2.open(filename);

		// increment file count
		files++;

		// initialize line counter
		lineno = 0;

		// clear errors in fin2 stream
		fin2.clear();

		// position at the beginning of the file to be searched
		fin2.seekg(0L, ios:: beg);

		// read each line from the file into buffer
		while (fin2.getline(buffer,sizeof(buffer))) {

			// increment line counter
			lineno++;

			// use strstr() to see if the line contains the target string
			if (strstr(buffer,argv[1])) {

				// If so, increment hit counter
				hits++;

				// display filename, line count and contents of line
				cout << filename << '[' << lineno << "] " << buffer << '\n';
			}
		}

		// close the file to be searched
		fin2.close();
	}

	// close the temporary file (containing names of files to be searched)
	fin1.close();

	// Print summary information
	cout << "Found " << hits << " occurrence(s) in " << files << " file(s)\n";

	// create the DOS command to erase the tempFilename
	strcpy(command,"erase ");
	strcat(command,tempFilename);

	// Issue system erase command
	system_command_status = system(command);

	// status system command
	if (system_command_status == -1) {
		cerr << "Error with system command: " << command << endl;
		exit(-5);
	}

	return 0;
}




CIS27: Programming in C++    Instructor: Joe Bentley