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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <fstream>
#include <string>
#include <direct.h>
using namespace std;
int z=0, a=0, slength=0, file_country[23151];
char fileNames[23151][20], country[23151][2];
char path[MAX_PATH+1], root[MAX_PATH+1], dirName[MAX_PATH+1],
realPath[MAX_PATH+1], tempFile[MAX_PATH+1];
bool done=0;
bool copyFile (const char SRC[], const char DEST[]) // copies a file's contents into another
{
ifstream src; // the source file
ofstream dest; // the destination file
src.open (SRC, ios::binary); // open in binary to prevent jargon at the end of the buffer
dest.open (DEST, ios::binary); // same again, binary
if (!src.is_open() || !dest.is_open())
return false; // could not be copied
dest << src.rdbuf (); // copy the content
dest.close (); // close destination file
src.close (); // close source file
return true; // file copied successfully
}
int rir(bool bCountHidden = false) //puts file names into the string fileNames[][]
{
WIN32_FIND_DATA fd;
DWORD dwAttr = FILE_ATTRIBUTE_DIRECTORY;
if(!bCountHidden) dwAttr |= FILE_ATTRIBUTE_HIDDEN;
sprintf( path, "%s\\*", root);
HANDLE hFind = FindFirstFile( path, &fd);
if(hFind != INVALID_HANDLE_VALUE)
{
int count=0,k=0;
do
{
if( !(fd.dwFileAttributes & dwAttr)){
strcpy(&fileNames[k][0],fd.cFileName);
strncpy(&country[k][0],fd.cFileName,2);
puts(fd.cFileName);
k++;
}
} while( FindNextFile( hFind, &fd));
FindClose(hFind);
return count;
}
return -1;
}
void Execution(string LOCATION){ // formats the file into the desired format
//----- ----------------------- WHILE YOU'RE IN THE FILE
int i=0;
ifstream input(LOCATION.c_str());
sprintf(tempFile,"%s\\tempFile",root);
ofstream output(tempFile, ios::trunc);
if(input.fail()){
cout << "Error opening file." << endl;
exit(1);
}
char String[301];
while(!output.eof()){
// --------------------------------
input.getline(String,12); // This block of code allows me to separate the pieces of information
output << String; // meshed together initially so that it becomes more readable
input.seekg(11);
input.getline(String,5);
output << " " << String;
input.seekg(15);
input.getline(String,3);
output << " " << String;
input.seekg(17);
input.getline(String,5);
output << " " << String;
input.seekg(20);
// -----------------------------
input.getline(String,300,'\n'); // going to manipulate the string
for(int a=0; a<sizeof(String); a++){
if(String[a]==('T' || 'S' || 'I')){
if(String[a]==('X' || 'T' || 'S' || 'I'))
String[a-1]=',';
String[a]=' ';}
}
output << String << endl;
}
input.close();
output.close();
if (!copyFile (tempFile, LOCATION.c_str()))
cout << "File could not be copied successfully";
else
cout << "File copied successfully!";
//-------------------- EXITING THE FILE
}
void Organize(string LOCATION){ // organizes the unformatted files into directories by country
for(slength=0; slength<23150;slength++){
sprintf(dirName,"%s\\%s",root,&country[slength][0]);
CreateDirectory(dirName,NULL);
}
for(slength=0; slength<23150;slength++){
done=0;
for(z=0;z<23150;z++){
if(strncmp(&fileNames[slength][0],&country[z][0],2)==0){
file_country[slength]=z;
done=1;
sprintf(path,"%s\\%s",root,&fileNames[slength][0]);
sprintf(dirName,"%s\\%s",root,&country[slength][0]);
sprintf(realPath,"%s\\%s",dirName,&fileNames[slength][0]);
MoveFile(path,realPath);
}
if(done=1) break;
}
}
}
int main(int argc,char** argv){
cout << "GHCND_obj v0.8\n\n" << endl;
cout << "Please give the path of the directory which contains .dly files, ghcnd-stations.txt, and ghcnd-countries.txt(no spaces)"<< endl;
cout << "(ie: z:\\kayiita\\GHCND_data\\DATA\\ghcnd_all):"<< endl;
cin >> root;
rir(0);
cout << "Now organizing files into their respective country directories\nPlease wait until the program terminates."<< endl;
Organize(root);
for(slength=0; slength<23150; slength++){
sprintf(realPath,"%s\\%s\\%s", root, &country[file_country[slength]][0],&fileNames[slength][0]);
Execution(realPath);
if(slength==0)
cout << slength+1 << "file has been manipulated." << endl;
else cout << slength+1 << " files have been manipulated."<< endl;
}
cout << "Objective I completed." << endl;
return 0;
}
|