Copy content between txt files.

Hi,

I am tryin to copy content from one txt file to another. Find and replace string in this content at one time. I am getting this error,

Unhandled exception at 0x7c812aeb in Rename_3.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0013fca8.


}
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	ifstream cFileR;
	ofstream cFileW;
	string line;

	cFileW.open ("write.txt");
	if (! cFileW.is_open())
		cout << "Unable to create c file"; 

	cFileR.open ("source.txt");
	if (cFileR.is_open())
	{
		while (!cFileR.eof())
		{
			getline(cFileR,line);
			line.replace(line.find("[name]"),6,"RTXApp");
			cFileW << line << endl;	
		}
	}
	else cout << "Unable to open c_source.txt file";

	cFileR.close();
	cFileW.close();

	return 0;
}
On this line:

line.replace(line.find("[name]"),6,"RTXApp");

You program will crash if .find() returns string::npos.
Topic archived. No new replies allowed.