Created
February 24, 2014 01:45
-
-
Save mickelsonm/9180435 to your computer and use it in GitHub Desktop.
simple/trivial exercise for doing a file IO task and sorting the results.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************** | |
* A demonstration of File IO and loops | |
* This program will read from a file, | |
* parse the line data into tokens. | |
* sort the data, and write to a file. | |
* | |
* Author: Matt Mickelson | |
* Original Files/Concepts: Dave Dalsveen | |
*/ | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <sstream> | |
#include <algorithm> | |
using namespace std; | |
const int ARRAY_SZ = 40; | |
struct Element{ | |
int number; | |
double atomicWeight; | |
string name; | |
}; | |
/** | |
* Used by the sort function to compare Element structs.. | |
*/ | |
bool compareAtomicWeight(const Element& elementOne, | |
const Element & elementTwo ){ | |
return elementOne.atomicWeight < elementTwo.atomicWeight; | |
} | |
/** | |
* The main function takes in the command line arguments. | |
* argc is the number of arguments. | |
* argv is an array of character strings. | |
*/ | |
int main(int argc, char *argv[]) { | |
string s; // A string to hold the input | |
int count = 0; | |
Element elementArray[ARRAY_SZ]; | |
string sArray[ARRAY_SZ]; | |
stringstream inputStr; | |
string token; | |
// Check for the proper number of command line args. | |
if (argc < 3) { | |
cout << "usage: FileIO inputfile outputfile" << endl; | |
return (EXIT_FAILURE); | |
} | |
// Outputs the command line | |
for (int i = 0; i < argc; i++) { | |
cout << "argv[" << i << "]=" << argv[i] << endl; | |
} | |
// Try to do the file IO | |
//ifstream inFile("c:/inFile.txt"); | |
// argv[1] is the input file name | |
ifstream inFile(argv[1]); | |
if (inFile.is_open()) { | |
//the sentinnel value is eof() | |
while (!inFile.eof()) { | |
//gets an entire line | |
getline(inFile, s); | |
inputStr << s; | |
int tokCnt = 0; | |
while ( getline(inputStr, token, ',') ){ | |
sArray[tokCnt] = token; | |
tokCnt++; | |
} | |
// store the tokens to the Element Array Element | |
elementArray[count].atomicWeight = atof(sArray[1].c_str()); | |
elementArray[count].number = atoi(sArray[0].c_str()); | |
elementArray[count].name = sArray[2]; | |
inputStr.clear(); | |
count++; | |
}//end while inFile | |
cout << "Line Count = " << count << endl; | |
inFile.close(); | |
//do the actual sort | |
sort(elementArray, elementArray + count - 1,compareAtomicWeight); | |
// output the sorted data to a file | |
ofstream outFile(argv[2]); | |
if (outFile.is_open()) { | |
for (int i = 0; i < count; i++) { | |
outFile << elementArray[i].name<< ":" <<elementArray[i].number <<":" | |
<< elementArray[i].atomicWeight <<"\r\n"; | |
} | |
outFile.close(); | |
}// end if output open | |
}// end if input open | |
else { | |
cout << "Input file can't be opened:" << argv[1] << endl; | |
return (EXIT_FAILURE); | |
} | |
return (EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment