#include #include using namespace std; void main() { //Tell them we're converting. cout << "Converting...\n\n"; //Open "input.txt" for reading. fstream fin("input.txt"); //Open "output.txt" for writing - the ios::out parameter is required. This will overwrite if applicable. fstream fout("output.txt", ios::out); //This will store each line of characters that we read - we'll set the max size of each line (column) to 100. char nextLine[100] = {0}; //These variables are used to separate every so many columns with a new line character (e.g. 25 columns per row). int columnCount = 0, columnMax = 25; //Make sure "input.txt" and "output.txt" were opened properly. if (fin && fout) { //This is an infinite loop - it will only break (exit) from the loop when we've reached the end of the file. while (true) { //Read the next line from the file. fin.getline(nextLine, 100); //Check to see if we've reached the end of the file. If so, break out of the reading/writing while loop. if (!fin) { break; } //Output the next column. fout << nextLine; //Another column is being added. ++columnCount; //Output a new line character after so many (columnMax) columns are outputted. if (columnCount >= columnMax) { //Output a new line character. fout << "\n"; //Reset the column count for the next row. columnCount = 0; } else { //Make sure we don't put a comma at the end of the file. if (fin) { //Output a comma. fout << ","; } } } //Assume success since there were no file opening errors. cout << "Conversion successful! Check \"output.txt\"."; } else { //The files were not opened properly - let the user know that it was unsuccessful. cout << "Error either opening \"input.txt\" for reading or opening \"output.txt\" for writing."; } //Close the files. fout.close(); fin.close(); //Output some new line characters before the program asks for a key press. cout << "\n\n"; system("pause"); }