Help needed.

I have to replace a string of a file by first opening it . the code i used is
In the file it is CONVERTED_REC_STATS = 7/or any number. i have to replace it with
CONVERTED_REC_STATS = UNK :7 ie i have to insert UNK in between CONVERTED_REC_STATS = and the number.so i have used this code..
#include<stdio.h>
#include <iostream>
#include <fstream>
#define T 8192
using namespace std;
char text[T];
int main()
{
string strReplace = "CONVERTED_REC_STATS";
string strNew = "CONVERTED_REC_STATS = UNK :";
string strReplace1 = "REC_KEY,ORIG_REC_KEY";
string strNew1 = " ""REC_KEY"",""ORIG_REC_KEY"" ";
std::ifstream filein("SKJ12032012.00112"); //File to read from
std::ofstream fileout("write2.txt"); //Temporary file
if(!filein || !fileout)
{
cout << "Error opening files!" << endl;
return 1;
}

string strTemp;
//bool found = false;
while(!filein.eof())
{
filein.getline(text,T);
string str(text);
int d = strncmp((char*)strReplace.c_str(),(char*)str.c_str(),19);
if(!d)
{
str = strNew;
//found = true;
}
//if(str == strReplace1){
//str = strNew1;
//found = true;
//}

str += "\n";
fileout << str;
//if(found) break;
}
return 0;
}

But the code is simply replacing and displaying CONVERTED_REC_STATS = UNK : and not the number specified .... so please help..........
i'm not sure but i think, after filein.getline(text,T), the entire line gets copied to text and further to str, i.e., "= 7" is also copied into str.
when you say str = strNew, the entire string just gets replaced by "CONVERTED_REC_STATS = UNK :". You will have to do character-wise replacement i think.
... or you can split the string str at "=" and say -

str = strNew + "2nd half of string";

but as i said, i'm not sure and i'm waiting for other responses myself.
Last edited on
yes that i know , but when i am doing that then in the output file result is coming but all the lines are not read as one line but as character and the entire format of input file is being changed.
i have no any Idea because i need also....
Topic archived. No new replies allowed.