Hi, Catfish3,
Hi, MiiNiPaa,
Thank you both for solving my dilemma.
Also, thank you for teaching me to use code tags.
Your suggestions fixed my problem.
Of course, as a noob, I have many more problems with my code.
After I fixed my code, as you suggested, I tried to insert the relevant portions into my project. The code worked, but I must have put it in the wrong place, because it caused my other code not to work!
The attached code (without the indented code in the middle - see lines 45 to 58 in the code tagged code below) works to read a user-entered text file and then output values from a map to a new text file. However, I need to make a copy of the user input file first, because I want to modify its contents, and I don't want to risk damaging the original file. (I want to remove the first five characters from each line of nine characters in the input file before testing them against the map. I also have to deal with a header in the input file, but that is too much for me right now.)
When I added the code to copy the file (indented in middle of code attached below), it copied the file to a new file as intended, but then the map routine, which worked fine without the new code, returned the error "Sorry '' Not in move list." How can I fix the code so that both parts work to (1) copy the input file to a new file, and (2) return the map values to the output file?
Here is the output from the console window.
PMAABQQQQ PMABWEHHE PMACBEEEE
Please enter the input file name and location> Testing.TXT
PMAABQQQQ
PMABWEHHE
PMACBEEEE
Sorry, `' not in move_list.
Process returned 0 (0x0) execution time : 9.391 s
Press any key to continue. |
Thanks for your help, MHL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
#include <map>
#include <string>
#include <fstream>
using namespace std;
int main()
{
std::map<std::string, std::string> move_list;
move_list["XXXXXXXXX"] = "ABCD";
move_list["YYYYYYYYY"] = "EFGH";
move_list["ZZZZZZZZZ"] = "IJKL";
for (std::map<std::string, std::string>::const_iterator ci = move_list.begin();
ci != move_list.end();
ci++
)
std::cout << (*ci).first << ' ';
std::cout << std::endl;
std::string TXT;
//ask user for file name
ifstream myfile;
string line;
ofstream copyfile ("Outputfile.txt");
cout << "Please enter the input file name and location> " << flush;
while (true)
{
myfile.close();
myfile.clear();
string myfilename;
getline( cin, myfilename );
myfile.open( myfilename.c_str() );
if (myfile) break;
cout << "Invalid file. Please enter a valid input file name and location> " << flush;
}
if (myfile.is_open())
//New code to copy input file to new file
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
{
copyfile << line << endl;
}
}
}
else cout << "Unable to open file";
//end of new code to copy input file
{
while(getline (myfile,TXT))
{
std::ofstream mynewfile ("Testing.TXT", ios::app);
if (mynewfile.is_open())
{
mynewfile << (*move_list.find(TXT)).second <<std::endl;
mynewfile.close();
}
else std::cout << "Unable to open file";
}
myfile.close();
}
else std::cout << "Unable to open file";
if (move_list.count(TXT) == 0)
std::cout << "Sorry, `" << TXT << "' not in move_list.";
else
std::cout << std::endl;
}
|