/* copyfile.cpp 

We are dealing with student records.
We ask the user for filename, the name of a file containing 
student records.
We copy each record from this file to the file cfilename
and print out the number of records copied.

*/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct student{
       string ssn;    // social security number
       string lname;  // lastname
       string fname;  // firstname
       int  mid1;
       int  mid2;
       int  final;
       int  hmwks;
};


int main()
{
 	string filename; // name of a file we read from
	string fileout;  // name of a file we write to
	int n;           // number of students in filename
	ifstream fin;    // input stream
	ofstream fout;   // output stream

	cout << "Enter name of input file: ";
	cin >> filename;
	fin.open(filename.c_str());
	if (!fin) {
		cout << "Sorry: cannot open " << filename << endl;
		return 1;
	}
	fileout = "c" + filename;
	fout.open(fileout.c_str());
	if (!fout) {
		cout << "Sorry: cannot open output file " << fileout << endl;
		return 1;
	}
	fin >> ws;  // the manipulator ws throws away white spaces
	for (n = 0; !fin.eof(); ++n) {
		student buffer;  // where we read a student record
		fin >> buffer.ssn >> buffer.lname >> buffer.fname >> buffer.mid1
			>> buffer.mid2 >> buffer.final >> buffer.hmwks;
		fin.ignore(256, '\n'); // this throws away junk that may exist on
		                       // the current line after the student data
		fin >> ws;
		fout << buffer.ssn << " " << buffer.lname << " " << buffer.fname << " " 
			<< buffer.mid1 << " " << buffer.mid2 << " " << buffer.final << " "
			<< buffer.hmwks << endl;
	}
	fin.close();
	fout.close();
	cout << "Copied " << n << " records" << endl;
	return 0;
}


