All pastes #43269 Raw Edit

lotus_anima

public cpp v1 · immutable
#43269 ·published 2006-02-26 23:04 UTC
rendered paste body
#include <iostream>#include <fstream>#include <cstdlib>#include <string>using namespace std;unsigned int linesInFile( ifstream& inStream );int largestInFile( ifstream& inStream );int smallestInFile( ifstream& inStream );long double averageOfFile( ifstream& inStream, unsigned int lines );int main(){	string line, inFile;	cout << "Please enter the name of the file you'd like to open: ";	cin >> inFile; cout << endl;	ifstream inStream( inFile.c_str() );	if( !inStream.is_open() ){		cerr << "Failed to open " << "file" << " for reading." << endl;		exit(1);	} else{		unsigned int lines = linesInFile(inStream);		cout << inFile << " opened." << endl;		cout << "Count of numbers: " << lines << endl;		if(lines > 1){			cout << "Largest number: " << largestInFile(inStream) << endl;			cout << "Smallest number: " << smallestInFile(inStream) << endl;		}	}	inStream.close();	if(inStream.is_open()){		cerr << "Failed to close " << inFile << "\n" << endl;		exit(1);	} else{		cout << inFile << " closed.\n" << endl;	}	return 0;}unsigned int linesInFile( ifstream& inStream ){	string line;	unsigned int lines = 0;	// Reset the file pointer	inStream.seekg(0);	while( getline(inStream, line) && !inStream.eof() ){		lines++;	}	return lines;}int largestInFile( ifstream& inStream ){	string line;	int largest;	// Reset the file pointer	inStream.seekg(0);	getline(inStream, line);	largest = atoi( line.c_str() );	while( getline(inStream, line) && !inStream.eof() ){		if( largest < atoi( line.c_str() ) ){			largest = atoi( line.c_str() );		}	}	return largest;}int smallestInFile( ifstream& inStream ){	string line;	int smallest;	// Reset the file pointer	inStream.seekg(0);	getline(inStream, line);	smallest = atoi( line.c_str() );	while( getline(inStream, line) && !inStream.eof() ){		if( smallest > atoi( line.c_str() ) ){			smallest = atoi( line.c_str() );		}	}	return smallest;}long double averageOfFile( ifstream& inStream, unsigned int lines ){	string line;	long int total;	// Reset the file pointer	inStream.seekg(0);	getline(inStream, line);	total = atoi( line.c_str() );	while( getline(inStream, line) && !inStream.eof() ){		total += atoi( line.c_str() );	}	return total / lines;}