1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
void openfile (string &filename, string filename2, ifstream &in,ofstream &out )
{
//Make an input file
//Note: filename is a string variable. Suppose user enters in myname.txt hello. I want to get rid of hello? and possibly not use hello to get stored in variable filename2. How can I?
cout<<"Enter Input File name: ";
cin>>filename;
//Convert the file in to c string
in.open(filename.c_str());
//make an output file
cout<<"\nEnter Output File Name: ";
cin>>filename2;
out.open(filename2.c_str());
}
void studenttestreader (int testscores[],int size,ofstream &out, ifstream&in)
{
//Total number of students not defined. There would be less then 50 though. So have numberofstudents declared to 0;
int numberofstudents=0;
in>>testscores[numberofstudents];
//while file is open
while (in>>testscores[numberofstudents] && numberofstudents<size)
{
in>>testscores[numberofstudents];
out<<testscores;
}
}
void closefiles(ifstream &in, ofstream &out)
{
in.close();
out.close();
}
int _tmain(int argc, _TCHAR* argv[])
{
//Declare variables
string filename, filename2;
ofstream output;
ifstream input;
//call function
openfile(filename,filename2,input,output);
//Call function to read the scores
int testscorereader[50];
studenttestreader(testscorereader,50,output,input);
//close the files
closefiles(input,output);
|