#include #include #include using namespace std; void main() { cout << "Formatting...\n\n"; fstream fin("input.txt"); fstream fout("output.txt", ios::out); if (fin && fout) { char nextLine[50000]; string fileContents = ""; fin.getline(nextLine, 50000); //Read the entire file into the fileContents string. while (fin) { fileContents += nextLine; fin.getline(nextLine, 50000); if (fin) { fileContents += "\n"; } } int fileLength = strlen(fileContents.c_str()); string fileOutputContents = ""; int digitNum = 0; for (int i = 0; i < fileLength; ++i) { //Completely skip the dashes. if (fileContents[i] != '-') { //If it's a new column, reset the digit count and output the new line or comma character like normal. if (fileContents[i] == '\n' || fileContents[i] == ',') { digitNum = 0; fileOutputContents += fileContents[i]; } else { //We're assuming it's a digit now. //Check to make sure that this either isn't the first digit in the number or that it's not a 1. if (digitNum > 0 || fileContents[i] != '1') { //If so, output the character. fileOutputContents += fileContents[i]; //Check to see if it's the third digit in the number; if so, output a comma. if (digitNum == 2) { fileOutputContents += ','; } //Increase the digit counter. ++digitNum; } } } } fout << fileOutputContents; cout << "Phone numbers formatted successfully! Check \"output.txt\"."; } else { cout << "Error either opening \"input.txt\" for reading or opening \"output.txt\" for writing."; } fout.close(); fin.close(); cout << "\n\n"; system("pause"); }