Re: Copy, Translate, and Save Text File
On Thursday, April 4, 2013 9:02:04 AM UTC-7, Paavo Helde wrote:
Michael Lester wrote >> >
I have code that will read the first line of my old file and write the
same line to my new file (Code 1). I also have separate code that will
read user input and replace the input using a std::map database (Code
2). I would like to know how to integrate Code 1 and Code 2 into one
project so that (a) the std:map (Code 2) will use the line read by
Code 1 from the old file to find the replacement text in the database
instead of asking for user input, then (b) Code 1 will write the new
text to the new file, and (c) the code will repeat to the end of the
file with a loop, or something.
Seems you already have all the pieces, you should just merge them together.
The final program should be something like:
populate map
open old and new files
while(getline(...)) {
process line and write to the new file
}
done
hth
Paavo
Hi, Paavo,
I am almost there, I think! Thank you for your help to get me this far.
The following code appears to read all lines from my input file, but only outputs the last line to my output file. I want to populate the output file with all the values for which there are keys in the input file, not just the last value.
Will you help me a little more, please?
Thanks,
Michael
#include <iostream>
#include <map>
#include <string>
#include <fstream>
using namespace std;
int main()
{
std::map<std::string, std::string> move_list;
move_list["AAAAAAAAA"] = "aaaa";
move_list["BBBBBBBBB"] = "bbbb";
move_list["CCCCCCCCC"] = "cccc";
for (std::map<std::string, std::string>::const_iterator ci = long_word.begin();
ci != long_word.end();
ci++
)
std::cout << (*ci).first << ' ';
std::cout << std::endl;
std::string XYZ;
std::ifstream myfile ("Test.XYZ");
if (myfile.is_open())
{
{
while (getline (myfile,XYZ));
std::cout << XYZ << std::endl;
}
myfile.close();
}
else std::cout << "Unable to open file";
if (long_word.count(XYZ) == 0)
std::cout << "Sorry, `" << XYZ << "' not in long_word.";
else
{
std::ofstream mynewfile ("Test.ABC", ios::app);
if (mynewfile.is_open())
{
mynewfile << (*long_word.find(XYZ)).second;
mynewfile.close();
}
else std::cout << "Unable to open file";
std::cout << std::endl;
}
}