//CopyFile - Copies the content of an input file to an //output file. #include #include #include using namespace std; typedef char* StringPtr; int GetFileNames(char* inputfilename, char* outputfilename, int &Start, int &Length){ cout << "Enter name of input file. \n"; cin.getline(inputfilename, 100); cout << "Enter starting position of key. "; cin >> Start; cout << "Enter length of key. "; cin >> Length; cout << "Enter name of output file. \n"; cin.get(); //Investigate - not reading next line properly cin.getline(outputfilename, 100); return 0; //Need to return error code. }//end GetFileNames void DisplayData(StringPtr Items[], int TopIndex){ for(int Posn=0; Posn<=TopIndex; Posn++){ cout << Items[Posn] << "\n"; }//end for }//end DisplayData int GetData(StringPtr Items[], char* inputfilename, ifstream &inputstream){ //open input file and inputstream.open(inputfilename); int TopIndex = -1; char bufr[100]; //buffer to hold each line data while(! inputstream.eof()) { //read lines into a buffer inputstream.getline(bufr, 100); StringPtr ps = new char[strlen(bufr)+1]; //allocate space //put a pointer to each line in array elements strcpy(ps, bufr); //copy string into new memory Items[++TopIndex] = ps; //put pointer into array }//end while inputstream.close(); return TopIndex; }//end GetData void PutData(StringPtr Items[], int TopIndex, char* outputfilename) { //Open output stream ofstream outputstream; outputstream.open(outputfilename); //write data to output file for(int Posn=0; Posn<=TopIndex; Posn++){ outputstream.write(Items[Posn], strlen(Items[Posn])); outputstream.write("\n",1); }//end for outputstream.close(); }//end PutData int main() { //Ask user for input file name //Check for existence of input file //Ask user for starting position and length of key //Ask user for output file name //Check for existence of output file //If exists then ask user if want to supercede. int ErrCode=-1; char inputfilename[100]; char outputfilename[100]; int Start; int Length; ifstream inputstream; //declare input stream ErrCode = GetFileNames(inputfilename, outputfilename, Start, Length); cout << "input file is " << inputfilename << "\n"; cout << "output file is " << outputfilename << "\n"; //cout << "Start and Length are " << Start << ", " << Length << "\n"; //cout << "Error Code is " << ErrCode << "\n"; //Open input file //Read in lines of data from input file into an array //close input file StringPtr Items[1000]; int TopIndex = GetData(Items, inputfilename, inputstream); //Display data in array DisplayData(Items, TopIndex); //Sort the array's items based on starting position //and length of key. //SelSort will call FindLargest and Swap repeatedly //to sort the data //FindLargest will need to call strcmpx to compare //from Start to Start+Length-1 //Display the data in array //Open output file //Write the data from the array to the output file. //close output file PutData(Items, TopIndex, outputfilename); return 0; }//end main