We want to use C++ to read a text file line by line, sort the content and write the result back to a new text file. We assume the file size is small enough to fit into the memory, several edge cases are not covered; although the power of STL will cover most of them! //See: http://www.cppreference.com/cppio/index.html //The library allows programmers to do file //input and output with the ifstream and ofstream classes #include #include #include #include using namespace std; int main(int argc, char* argv[]) { const static char* InputFilePath = argv[1]; const static char* OutputFilePath = argv[2]; //At least two arguments must be provided if(argc != 3) { cout << "The first argument is the input file path," " and the second one is the output file path" <<> setFileData; //Read Data ifstream fileInput(InputFilePath); copy( istream_iterator (fileInput) , istream_iterator () , inserter(setFileData, setFileData.begin()) ); //Write Sorted Data ofstream fileOutput(OutputFilePath); copy( setFileData.begin() , setFileData.end() , ostream_iterator(fileOutput, "\n")); return 1;
}
No comments:
Post a Comment