http://thecodingtutorials.blogspot.com/2013/01/simple-file-manipulation-in-console.html
http://thecodingtutorials.blogspot.com/2013/01/character-arrays-in-c.html
This tutorial will show a C++ program that takes input from a file called "input.txt" and outputs it to "output.txt", only this time, it will take input like this:
1234567
1234567
1234567
and output it with commas after the third digit on each line like this:
123,4567
123,4567
123,4567
Here's a screenshot of our fully finished program working:
and here are some useful links for you in case you want them:
Code file ("main.cpp"): http://www.thecodingwebsite.com/tutorials/commaadder/main.cpp
Code file as a ".txt" file ("main.txt") so you can view it in your browser: http://www.thecodingwebsite.com/tutorials/commaadder/main.txt
Finished program: http://www.thecodingwebsite.com/tutorials/commaadder/CommaAdder.exe
Sample input file ("input.txt"): http://www.thecodingwebsite.com/tutorials/commaadder/input.txt
Sample output file ("output.txt") (what the output should look like after running the program): http://www.thecodingwebsite.com/tutorials/commaadder/output.txt
The program we're writing is extremely similar to that of one of my previous tutorials, so I'll use that as a baseline and modify just a few things. The bulk of the program is the same, however there will be some slight changes and one improvement.
The beginning of the program is nearly the same, except that we're going to include the string library and use a string variable to store the next line this time.
#include <iostream> #include <fstream> #include <string> using namespace std; void main() { //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); //Make sure "input.txt" and "output.txt" were opened properly. if (fin && fout) { //Tell them we're adding commas. cout << "Adding commas...\n\n"; //This will hold each line of characters that we read and write. string nextLine = ""; //This is an infinite loop - it will only break (exit) from the loop when we've reached the end of the file. while (true) {
Rather than using "fin.getline" to read the next line, we will use the generic "getline" function with "fin" as the first parameter:
//Read the next line from the file. getline(fin, nextLine);
This way, we can use a string for file input rather than having to mess with a character array.
The rest of the while loop deals with checking for the end of the file (as before), adding the comma (the main goal of the program, which I will address shortly), and then outputting each revised line:
//Read the next line from the file. getline(fin, nextLine); //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; } if (nextLine.length() > 2) { nextLine.insert(3, ","); } //Output the next line. fout << nextLine << '\n'; }
Now, let's take a closer look at the comma insertion code above:
if (nextLine.length() > 2) { nextLine.insert(3, ","); }
First, I make sure that the next line's length is at least 2 characters long, because if it's anything less we won't bother adding a comma. Then I use the string's insert function to insert a comma at position 3 in the string. Don't forget that counting starts at 0, so we're making a comma be the 4th character in the string (as wanted) and then pushing the remainder over.
Here's the entire code for our finished program:
#include <iostream> #include <fstream> #include <string> using namespace std; void main() { //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); //Make sure "input.txt" and "output.txt" were opened properly. if (fin && fout) { //Tell them we're adding commas. cout << "Adding commas...\n\n"; //This will hold each line of characters that we read and write. string nextLine = ""; //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. getline(fin, nextLine); //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; } if (nextLine.length() > 2) { nextLine.insert(3, ","); } //Output the next line. fout << nextLine << '\n'; } //Assume success since there were no file opening errors. cout << "Adding commas 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"); }
No comments:
Post a Comment