-2

My task is to search for a string in .c file and modify it using c++ code. Iam done till searching for the string but modifying it is giving an error. It gives the same error if i copy the contents of c file to a text file and try to modify it. So iam sure something is wrong with my code. Please help as iam a beginner. Thanks in advance. My code:

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

using namespace std;

int main()
{
string s1, s2;

  ifstream test("test.csv");

  while (test.eof()==0)      //test.eof() returns 0 if the file end is not reached
  {
    getline(test, s1, ',');     //reads an entire line(row) till ',' to s1

    getline(test, s2, '\n');

    cout << s1 + "= " +s2 << endl;

    fstream fileInput;

    int offset;

    string line;

    string search=s1;

    fileInput.open("IO_CAN_0_User.c");

if(fileInput.is_open()) {

    while(!fileInput.eof()) {

        getline(fileInput, line);

        if ((offset = line.find(search, 0)) != string::npos) {

            cout << "found: " << search << endl;
            string str;
            str=search;
            str.replace(str.begin()+25,str.begin()+31,"=s2  //");
            break;
        }

    }
    //cout << "string not found" << endl;
    fileInput.close();
}
else cout << "Unable to open file.";


if(test.eof()!=0)
    cout<<"end of file reached"<<endl;
    getchar();
    return 0;
  }
}
  • 1
    `but modifying it is giving an error` You don't think the error might be an important part of the question? – John3136 Mar 09 '16 at 06:07
  • `while (test.eof()==0) //test.eof() returns 0 if the file end is not reached` not quite right. it means that the end of file has not yet been reached. The very next read could find the end of file, rendering that read invalid. Read more here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Mar 09 '16 at 06:12
  • Many sourcecode editors support search&replace, often even spanning multiple files. Do you really want to do this yourself? – Ulrich Eckhardt Mar 09 '16 at 06:12
  • Error:Debug assertion failed. Expression: string iterator+offset out of range – Naveen Umashankar Mar 09 '16 at 06:19
  • Ulrich Eckhardt: yes sir, iam sure i wanna do this myself. – Naveen Umashankar Mar 09 '16 at 06:21
  • Here: `string str; search=str;` you create an empty string. In the line after the next line, you try to access `str.begin() + 25`, whoch is clearly illegal for an empty string. (Where do the 25 and the 31 come from?) – M Oehm Mar 09 '16 at 06:23
  • I think the problem here is the replace function i have used. Please let me know how to write to the file at that particular location. I even tried fileInput << s3 << endl; it doesnt give any error but the file isnt updated too. – Naveen Umashankar Mar 09 '16 at 06:23
  • Thanks M Oehm, there has been a mistake, it is string str; str= search; I changed it, but it still gives the same error. – Naveen Umashankar Mar 09 '16 at 06:26
  • Look at Matteo's answer: You are using some strange magic numbers here, which would only make sense if your search string were hard-coded, which, by the looks of it, it isn't. – M Oehm Mar 09 '16 at 06:27

1 Answers1

1

The error your are facing is not clear, but I can see one big issue, your running replace on an empty string.

Your code:

string str;
search=str;
str.replace(str.begin()+25,str.begin()+31,"=s2  //");

You create str (by default initialized as empty string), assign it to search (therefore this string gets empty) and then you call replace trying to change from char 25 to 31, which are not there since the str is empty.

Update
Probably you need to fix the replace, but then you cannot expect the file to change: the string you are modifying is in memory, not a piece of your file.

Therefore I would change the code (using yours as much as possible):
* Adding output file
* Fixing the replace
* Saving every line of the input file (replacing if need) on the output

fileInput.open("IO_CAN_0_User.c");
ofstream  fileOutput;
fileOutput.open("output.c");

if(fileInput.is_open() && fileOutput.is_open() ) {

  while(!fileInput.eof()) {

    getline(fileInput, line);

    if ((offset = line.find(search, 0)) != string::npos) {

        cout << "found: " << search << endl;

        line.replace( offset, offset+search.size(), s2 );
    }

    fileOutput << line << '\n';
}
Matteo
  • 447
  • 2
  • 10
  • Its str=search; I updated it, still the same error. Error:Debug assertion failed. Expression: string iterator+offset out of range – Naveen Umashankar Mar 09 '16 at 06:29
  • Is there any function that i can use instead of s.replace()? – Naveen Umashankar Mar 09 '16 at 06:34
  • @NaveenUmashankar: You need to understand your own code better. `str`, the string on which you do the `replace`, goes out of scope immediately. And you determine offset and never use it. Tidy up your `string` definitions; you have only three strings: The current line, the search string and its replacement. – M Oehm Mar 09 '16 at 06:37
  • There was a typo ( offset+s1.size() => offset+search.size()). – Matteo Mar 09 '16 at 06:42
  • Isn't der any way where i don't have to create a new file altogether? Its quite a large file. – Naveen Umashankar Mar 09 '16 at 06:42
  • Yes you could, but it is more complex. You can use fseek and fputs (C API) – Matteo Mar 09 '16 at 06:47