148 lines
5.0 KiB
Plaintext
148 lines
5.0 KiB
Plaintext
Listing PG6.cpp...
|
|
|
|
// Ben Harris
|
|
// Main: Program6
|
|
// This program reads a file, uses the first four bytes as an integer record size and sorts the rest of the file in recordsz chunks.
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <cstdlib>
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include "PG6.h"
|
|
using namespace std;
|
|
|
|
int main(int argc, char **argv){
|
|
string fn = "";
|
|
streampos fsz;
|
|
cout << "Enter the name of the file that you want to sort." << endl << "File Name: ";
|
|
getline(cin, fn); // Get file name from user
|
|
fstream f (fn, ios::in | ios::out | ios::binary); // Open file from input location for read and write and as binary
|
|
if (f.is_open()){
|
|
|
|
char * recordsz = new char[4];
|
|
f.read(recordsz, (streamsize) 4); // Read first four bytes
|
|
int rsz = atoi(recordsz); // Store first four bytes as int record size
|
|
f.seekg(0, f.end);
|
|
fsz = f.tellg(); // Get length of file
|
|
int recordcnt = ((int)fsz-4) / rsz; // Get # of records
|
|
f.seekg(0, f.beg);
|
|
|
|
// Begin shell sort
|
|
int d = recordcnt;
|
|
while (d > 1){
|
|
d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2;
|
|
for (int i = d; i < recordcnt; i++) // Use readstring and writestring methods to get and put strings in the file
|
|
for (int j = i - d; j >= 0 && readstring(f,j,rsz) > readstring(f,j + d,rsz); j -= d){
|
|
string t = readstring(f,j,rsz);
|
|
writestring(f, j, rsz, readstring(f,j + d,rsz));
|
|
writestring(f, j+d, rsz, t);
|
|
}
|
|
}
|
|
cout << "Sort successful." << endl;
|
|
f.close();
|
|
}
|
|
else cout << "Error: file could not be opened or does not exist." << endl;
|
|
system("pause");
|
|
}
|
|
|
|
string readstring(fstream &f, int recordpos, int len){ // This method seeks to the position of recordpos * len, reads len bytes and returns that string
|
|
char* t = new char[len];
|
|
f.seekg(recordpos*len + 4, ios::beg);
|
|
f.read(t, len);
|
|
return (string)t;
|
|
}
|
|
|
|
void writestring(fstream &f, int recordpos, int len, string s){ // Writes string s to f at position recordpos*len
|
|
f.seekp(recordpos*len + 4, ios::beg);
|
|
char * t = new char[s.length()];
|
|
for (unsigned int i = 0; i < s.length(); i++) t[i] = s[i];
|
|
f.write(t, len);
|
|
}
|
|
|
|
|
|
Listing PG6.h...
|
|
|
|
// Ben Harris
|
|
// Header for program 6
|
|
// This program reads a file, uses the first four bytes as an integer record size and sorts the rest of the file in recordsz chunks.
|
|
|
|
#ifndef _PG6_
|
|
#define _PG6_
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <cstdlib>
|
|
#include <fstream>
|
|
using namespace std;
|
|
|
|
int main(int argc, char **argv);
|
|
string readstring(fstream &f, int recordpos, int len);
|
|
void writestring(fstream &f, int recordpos, int len, string s);
|
|
|
|
#endif
|
|
|
|
|
|
Microsoft Windows [Version 6.1.7601]
|
|
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG6.exe
|
|
Microsoft Windows [Version 6.1.7601]
|
|
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
|
|
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG6.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG6.exe
|
|
PG6.cpp
|
|
Microsoft (R) Incremental Linker Version 10.00.40219.01
|
|
Copyright (C) Microsoft Corporation. All rights reserved.
|
|
|
|
/out:PG6.exe
|
|
/OUT:PG6.exe
|
|
PG6.obj
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
|
|
Copyright (C) Microsoft Corporation. All rights reserved.
|
|
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test1.txt" .
|
|
1 file(s) copied.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-1.in"
|
|
Enter the name of the file that you want to sort.
|
|
File Name: Sort successful.
|
|
Press any key to continue . . .
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>cmp test1.txt C:\Grading\Classes\CS201-02-14F\PG6\test1.out
|
|
Files are the same.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>
|
|
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test2.txt" .
|
|
1 file(s) copied.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-2.in"
|
|
Enter the name of the file that you want to sort.
|
|
File Name: Sort successful.
|
|
Press any key to continue . . .
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>cmp test2.txt C:\Grading\Classes\CS201-02-14F\PG6\test2.out
|
|
Files are the same.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>
|
|
C:\Users\apoe\Desktop\Grading Folder>copy /y "C:\Grading\Classes\CS201-02-14F\PG6\test3.txt" .
|
|
1 file(s) copied.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>PG6.exe < "C:\Grading\Classes\CS201-02-14F\PG6\PG6-3.in"
|
|
Enter the name of the file that you want to sort.
|
|
File Name: Sort successful.
|
|
Press any key to continue . . .
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>cmp test3.txt C:\Grading\Classes\CS201-02-14F\PG6\test3.out
|
|
Files are the same.
|
|
|
|
C:\Users\apoe\Desktop\Grading Folder>
|
|
C:\Users\apoe\Desktop\Grading Folder>exit
|
|
|
|
50/50.
|