-1

i'm trying to replace line in file.

Example

aaa bbb ccc
bbb ccc ddd
ccc ddd eee

I want to replace second line with something like

111 222 333

so result will be

aaa bbb ccc
111 222 333
ccc ddd eee

I tried

while (getline(infile, curline))
{
    if (counter == line)
    {
        outfile << input1 << "\t" << input2 << "\t" << input3 << "\t" << input4 << endl;
        break;
    }
    counter++;
}

where line is number of line i want to replace.

Thanks for help!

Brky
  • 59
  • 11
  • @pingul It's not, i am replacing line and i am finding it with number of that line, not starting word. – Brky Apr 19 '17 at 11:12
  • 1
    Sounds like a trivial problem to solve if you start with the answer provided there. – pingul Apr 19 '17 at 11:14

1 Answers1

0

Files are not "just piece of memory on the hard disk". So, if you plan to change the file, you have to create new one with altered content, then rename it to the name of the old file (with deletion of the old file, of course). So, try to modify your code to something like that:

while (getline(infile, curline))
{
    if (counter == line)
    {
        // altered line creation
        outfile << input1 << "\t" << input2 << "\t" << input3 << "\t" << input4 << endl;
    }
    else
    {
        // the line goes without changes
        outfile << curline << endl;
        counter++;
    }
}
Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42