CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2013
    Posts
    11

    urgent 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:fstream 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..........

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: urgent help needed

    First you code is very hard to read/understand. The reason is you didn't use Code tags.
    Second, why do you use strncmp C-runtime to work with the std::string rather than using std::string::replace method?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: urgent help needed

    After you have found the string to be changed, where do you append the number to the new string????

    Code:
    if(!d)
    {
    str = strNew;
    //found = true;
    }

  4. #4
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Re: urgent help needed

    Quote Originally Posted by ramsavi View Post
    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.
    If you only need to do this once, you could just use a text editor.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: urgent help needed

    I suspect it's a homework exercise?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured