vendredi 20 février 2015

Replacing symbols with spaces in a string

I'm writing a program that can format text files of thousands of lines of data. The input text file are lines such as:



2010-01-01 00:00:00 0.520368 0.558565 0.567376



What I want to do is replace the dashes and ':' with spaces, and then put proper tabs in. I figured out how to fix the tab problem, but my algorithm for replacement just does not work. Any help is greatly appreciated. My code currently looks like this:



void getReportInfo(ifstream& file, ofstream& ofile)
{
string date, time;
double wheight1, wheight2, wheight3;


while(!file.eof())
{
file >> date >> time >> wheight1 >> wheight2 >> wheight3;

//replace dashes
for(int i = 0; i < date.length(); i++) {
if(date[i]== '-')
date[i] == ' ';
}
ofile << date << " ";

//replace colons
for(int i = 0; i < time.length(); i++) {
if(time[i]== ':')
time[i] == ' ';
}
ofile << time << " ";

ofile << "\t" << wheight1 << " \t" << wheight2 << " \t" << wheight3 << endl;
}
}

Aucun commentaire:

Enregistrer un commentaire