0

Lets say I have a code :

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string line,wantedString,newString;
    fstream subor("test.txt");
    while (!subor.fail())  // read line from test.txt
    {
        getline(subor,line);
        line.substr(line.find("?")+1);
        wantedString=line.substr(line.find("?")+1); // will take everything after '?' character till 
'\n'
    }
    cout<<"Enter new text to replace wantedString :";
    getline(cin,newString);

    // how to use string::replace() please ?
    /I tried this but does not work
    getline(subor,line);
    line.replace(wantedString,string::npos,newString); 
    return 0;
}

In test.txt is written only one line :

something?replace 

note: there is no '\n' in the file error thrown by compiler is :

    error: no matching function for call to 'std::__cxx11::basic_string<char>::replace(std::__cxx11::string&, const size_type&, std::__cxx11::string&)'

can you please answer working code with commented explaining why is it like you did it ?

I have studied string::replace() method here: http://www.cplusplus.com/reference/string/string/replace/

Is my logic of using string::find() as a starting point for string to be replaced ?

1 Answers1

1

Is my logic of using string::find() as a starting point for string to be replaced ?

Yes, but then you threw away the iterator/index result of find and went to get the substring instead.

Replace doesn't take a string.

It takes an iterator/index.

So just pass what you got from find, into replace. (Be careful of edge cases! Check for errors! Read the documentation for both functions.)

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    fstream subor("test.txt");

    string line;
    while (getline(subor,line))
    {
        // Find the index of the character after the first '?'
        const size_t wantedStringPos = line.find("?")+1;

        // Prompt for a replacement string
        cout << "Enter new text to replace wantedString: ";
        string newString;
        getline(cin,newString);

        // Perform the replacement
        line.replace(wantedStringPos, string::npos, newString);

        // Now do something with `line`
        // [TODO]
    }
}

(I've also fixed an off-by-one error in your loop.)

You then need to actually write the new, modified string back to the file: the file doesn't automatically get updated in sync with the copy of the data you previously read out from it.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
  • so i CHANGED 'string wantedString' to 'int wantedString = line.find("?")+1', ADDED 'string replace = line.replace(wantedString,string::npos,newString)' AND tried to update file WITH 'subor< – Ivo KRENO Durec Nov 15 '19 at 17:08
  • @IvoJOKERDurec You're still passing a string to `replace`. You should read the documentation to find out how to use `replace`. And for formatting assistance click the "help" link next to the comment box; it'll teach you how to make it readable. – Lightness Races in Orbit Nov 15 '19 at 17:38
  • i do not understand ... what should i pass there ? on the link i posted is written that the third argument is string, or am i missing something ? – Ivo KRENO Durec Nov 15 '19 at 17:45
  • @IvoJOKERDurec And what about the first argument? – Lightness Races in Orbit Nov 15 '19 at 17:47
  • the first argument is "Another string object, whose value is copied." from http://www.cplusplus.com/reference/string/string/replace/ and i tried to change string to size_t of wantedString, but this does not work either ... – Ivo KRENO Durec Nov 17 '19 at 13:27
  • @IvoJOKERDurec That is not the first argument, for any of the overloads. The "Arguments" section of that page is not in the order you give the arguments. Look at the declarations at the top of the page for the available combinations. Then look up the _name_ of each argument in the "Arguments" section for more information on how to provide it. – Lightness Races in Orbit Nov 17 '19 at 15:36
  • can you please just tell me what am i missing ? i tried to figure it out by your hints but still no result ... i also found and tried this answer https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string but this worked only when i run it in program ... without file included. when i tried to getline from file and find "?", it did not replaced within the file even if it compiled ... isn't problem in saving edited file somehow ? Maybe it works, but it will not save changes to the file. What do you think ? – Ivo KRENO Durec Nov 18 '19 at 14:14
  • @IvoJOKERDurec I've added a sketch. There are a few logical errors with your original code – Lightness Races in Orbit Nov 18 '19 at 14:41
  • so in while loop you have made const ... i tought i cant make a const while program is running and when i cout< – Ivo KRENO Durec Nov 18 '19 at 15:09
  • @IvoJOKERDurec I made `wantedStringPos` `const`, because it doesn't need to _not_ be `const`. I didn't have to do that, but it's a good habit for anything that doesn't need to change after initialisation. – Lightness Races in Orbit Nov 18 '19 at 15:58
  • @IvoJOKERDurec _"still when i want to write it in file like subor< – Lightness Races in Orbit Nov 18 '19 at 15:58