Click to See Complete Forum and Search --> : How to read a file char by char and replace it with string????


handytxg
May 31st, 2008, 05:08 AM
Hi, I need to convert english to morse code, atm Im using iterator and store the morsecode in vector. and store the result on new vector the problems is it'doesn't do the line properly.
when I try to change the itr instead of store to new vector i't give me error because I try to replace single char with morse code which is a string. So does any1 can tell me how to do it properly???
thanks

Hermit
May 31st, 2008, 08:36 AM
Why not create a second vector/string a push_back each character of the morse code? You could even write an algorithm that takes two input iterators and an output iterator, and pass it a back_insert_iterator for a string or vector as the output iterator.

To replace a character with several characters in-place in a vector, you'd need to use vector::insert (http://www.cplusplus.com/reference/stl/vector/insert.html) and take care that the original characters are removed from the vector, bearing in mind that frequent insertion/removal from the middle of a vector is not considered ideal. To do this with a string, you could simply use string::replace (http://www.cplusplus.com/reference/string/string/replace.html).

Hopefully that's all clear, but since you haven't provided the code for your attempt so far, I won't provide any either.

handytxg
May 31st, 2008, 10:33 AM
My code is like this:
I put the morse code in 1 vector, char for english and string for morsecode.
Im not sure it's a correct way to store data,any idea to make it better?
I'll try your solution thank you so much for your reply.


#include <iostream>
#include <vector>
#include <fstream>
#include <list>
#include <algorithm>

using namespace std;

vector<string> vectorresult;

class vectordata
{
private:
char english;
string morse;

public:
vectordata()
{

}

vectordata(const vectordata &other) :
english(other.english),
morse(other.morse)
{
// copy constructor
}

void setvectordata(const char &e, string m) {
english = e;
morse = m;
}

const char& getEnglish() const {
return english;
}

const string& getMorse() const {
return morse;
}

void printEmployee() const {
cout << " " << morse << " ";
}

void loadEmployee(ifstream &fin) {
fin >> english;
fin >> morse;
}

};

void loadList2(list<char> &charList, const char *file)
{
// load english text to be converted into link list

ifstream fin;
fin.open(file);

if (!fin) {
cout << "ERROR - unable to read " << file << "\n";
exit(0);
}

char ch;
while (!fin.eof()) {
fin.get(ch);
charList.push_back(ch);
}
fin.close();
}

void encode(list<char> &charList, vector<vectordata> &myvector)
{
list<char>::iterator itr;
vector<string>::iterator vitr;

unsigned int k;
char en;

for (itr = charList.begin(); itr != charList.end(); itr++)
{
en = *itr;
for (k=0; k<myvector.size(); k++)
{
if(en==myvector[k].getEnglish())vectorresult.push_back(myvector[k].getMorse());;
//*itr = myvector[k].getMorse();//doesn't work
}
}
}

void printList(const list<char> &charList)
{
// print the contents of the linked list

list<char>::const_iterator itr;

for (itr = charList.begin(); itr != charList.end(); itr++) {
cout << *itr;
}
cout << "\n";
}

void loadList(vector<vectordata> &myvector)
{
ifstream fin;
vectordata vd;

fin.open("morsecodes.txt");
while (!fin.eof())
{
vd.loadEmployee(fin);
myvector.push_back(vd);
}

fin.close();
}

void printvectordata(const vector<string> &vectorresult, const char *save)
{

ofstream myfile (save);
if (myfile.is_open())
{
unsigned int k;
for (k=0; k<vectorresult.size(); k++) {
myfile << vectorresult[k]; }
myfile.close();
}
else cout << "Unable to open file";
}

int main(int argc, char *argv[])
{
list<char> charList;
vector<vectordata> myvector;

if (argc != 4) {
printf("Syntax : euro file\n");
return 0;
}

cout << argv[1];
if ( *argv[1] == 'e' )
{
loadList2(charList, argv[2]);
//printList(charList);
loadList(myvector);
encode(charList, myvector);
printvectordata(vectorresult, argv[3]);
return 0;
}
else
{
cout << "error" << endl;
return 0;
}
}