Hello tgrump,
Welcome to the forum. I think you tried to use code tags, but did it wrong. Use the following links to read about code tags.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
Your program is a good start and there are a couple parts that are correct, but sorry to say most of the program is wrong.
For working with files you can start here:
http://www.cplusplus.com/reference/fstream/fstream/
For the do/while loop:
http://www.cplusplus.com/doc/tutorial/control/
You have opened the input and output files, but never check to see if they are open. If the input files does not open you will never know it and nothing will ever be read from the file stream. As I look at the code you never actually read from the input file. What I believe you are thinking are read statements are not. The line
if (inputFile > 0)
is actually comparing the file stream to zero. Not what you want.
"inputFile" and "outputFile" are file streams. They are what you read from and write to. Usually a read would be
inputFile >> aVariable;
. There are other ways of using "inputFile" to read from.
The use of "outputFile" is the correct usage for what you want.
Something you should do after opening a file stream is to check that it did open. When I was first learning about file streams I came up with this bit of code that I found useful. In the end it can be changed and shortened when you have a better understanding of using files.
For the input file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
std::string iFileName{ "" }; // <--- Put file name here.
std::ifstream inFile;
inFile.open(iFileName);
if (inFile.is_open())
{
std::cout << "\n File " << iFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << iFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1);
}
|
For the output file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
std::string oFileName{ "" }; // <--- Put file name here.
std::ofstream outFile;
outFile.open(oFileName, std::ios::trunc | std::ios::ate);
//outFile.open(oFileName, std::ios::app);
if (outFile.is_open())
{
std::cout << "\n File " << oFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << oFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1); // <--- No need to continue any further.
}
|
For line 5 of the output file I use the "trunc" and "ate". I found that this is the pair that works together. After the program is working properly I reverse the comments on lines 5 and 6 which opens the file for appending.
I will give you a chance to fix your code first. If you have any questions or problems let me know and we can work on them.
Hope that helps,
Andy
P.S. I forgot to mention, if you could include a sample of the input file it would help me to help you in reading the file.