Re: Copy, Translate, and Save Text File
On Wednesday, April 3, 2013 8:59:52 AM UTC-7, Paavo Helde wrote:
For the dictionary you will want to create and populate a std::map or
something like that for fast lookup of the words.
On Tuesday, April 2, 2013 9:01:42 PM UTC-7, Michael Lester wrote:
Question: How to copy user designated text file containing vocabulary
list, translate its contents, and save the new file to the same
directory as original file with a new file extension.
Sorry for the bad snip job...
Paavo has helped me understand more about how to do what I want to do, and =
I am hoping for some additional guidance.
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 use=
r input and replace the input using a std::map database (Code 2). I would l=
ike 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.
Here are the two codes I have:
Code 1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("Test.old");
if (myfile.is_open())
{
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
ofstream mynewfile ("Test.new");
if (mynewfile.is_open())
{
mynewfile << line;
mynewfile.close();
}
else cout << "Unable to open file";
return 0;
}
Code 2
#include <iostream>
#include <map>
#include <string>
int main()
{
std::map<std::string, std::string> database;
database["ABCDEFGHI"] = "1234";
database["BCDEFGHIJ"] = "2345";
database["CDEFGHIJK"] = "3456";
std::cout << "What word do want to find?\nAvailable: ";
for (std::map<std::string, std::string>::const_iterator ci = database.be=
gin();
ci != database.end();
++ci
)
std::cout << (*ci).first << ' ';
std::cout << std::endl;
std::string name;
std::getline(std::cin, name);
if (database.count(name) == 0)
std::cout << "Sorry, `" << name << "' not in database.";
else
std::cout << name << ": " << (*database.find(name)).second;
std::cout << std::endl;
}