<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>replace</title>
</head>

<body>



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></font></span>
<font size="6" color="#FF0000"><b><span lang="en-ca">Replace (little tool 
intended for UNIX)</span></b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This is a little tool I want to use on UNIX. As you know, we have &quot;grep&quot; to find string among files, but we </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>cannot replace string in files. (At least I don't know it.) So, I write this little tool for possible usage</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>in near future.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <font FACE="TimesNewRomanPSMT">
  <p ALIGN="LEFT"><span lang="en-ca">To find and replace the very first 
  occurrence for a target string in a file. I only allow the first replacement.</span></p>
  </font>
  <p ALIGN="LEFT">กก</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  กก</div>
<div align="left">
  <pre><span lang="en-ca">You need to find the string and you need to create another &quot;reading&quot; file pointer to copy everything you read back to file.</span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>There are few points to mention.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>The UNIX is a pain! Make sure you have the correct representation of punctuations! i.e. </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>./replace testfile \&quot;sources_tring\&quot;  \&quot;target.with_quotes\&quot;</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>See the quotes must be used like this and I removed the restriction of &quot;non-alpha-numeric&quot;. So, any string can</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>be replaced now. Another dirty part of UNIX is the &quot;new line&quot; character. I highly suspect that it is the reason</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>for me to debug for more than two hours!!! So, I simply add an extra condition for that.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left" style="width: 898; height: 86">
  <pre><font size="3"><b><span lang="en-ca">1. </span>main.cpp (main)</b></font></pre>
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: main</b></font></span><font size="3" color="#FF0000"><b>.<span lang="en-ca">cpp</span></b></font></pre>
  <pre>#include &lt;stdlib.h&gt;
#include &lt;ctype.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

const int BackOffSet=20;
const int MaxStringLength=BackOffSet+BackOffSet;

bool getWord(FILE* stream, char* buffer, const char* delimeter);

/*
int main(int argc, char *argv[ ])
{
*/
//the input string must be trimmed which means the white space 
//at both side would be ignored. i.e. &quot; string &quot; will be not matched 
//with &quot;string&quot; unless it is input with &quot;string&quot;.
//int main()
//int main(int argc, char *argv[ ])
int main(int argc, char *argv[ ])
{
	//int argc=4;
	//char *argv[ ]={&quot;replace&quot;, &quot;nick.txt&quot;, &quot;that&quot;, &quot;whywhywhywhy&quot;};
	FILE* stream;
	FILE* reader;
	int front, rear;
	char ch;
	char buffer[MaxStringLength];
	char line[MaxStringLength];
	int len;
	long pos;
	if (argc!=4)
	{
		printf(&quot;usage: replace filename source target\n&quot;);		
		exit(1);
	}
	if ((stream=fopen(argv[1], &quot;r+w&quot;))==NULL)
	{
		printf(&quot;cannot open file %s\n&quot;, argv[1]);
		exit(2);
	}
	len=strlen(argv[2]);
	while (!feof(stream))
	{
		//fscanf(stream, 
		//fgets(buffer, MaxStringLength, stream);
		getWord(stream, buffer, &quot; \t\0\n&quot;);
		if (strcmp(buffer, argv[2])==0)
		{
			pos=ftell(stream);
			fseek(stream, -BackOffSet, SEEK_CUR);
			fgets(line, MaxStringLength, stream);
			printf(&quot;replace string '%s' in '%s'\n(y/n) &gt; &quot;, argv[2], line);
			scanf(&quot;%c&quot;, &amp;ch);
			if (ch=='y')
			{
				fseek(stream, pos-len-1, SEEK_SET);
				reader=fopen(argv[1], &quot;r&quot;);
				fseek(reader, pos-1, SEEK_SET);
				strcpy(buffer, argv[3]);
				rear=strlen(buffer)-1;
				front=0;
				while (front!=rear)
				{
					if (!feof(reader))
					{
						rear=(rear+1)%MaxStringLength;
						buffer[rear]=getc(reader);
					}
					putc(buffer[front], stream);
					front=(front+1)%MaxStringLength;
				}
				fclose(stream);
				break;
			}
		}
	}
	return 0;
}
bool isDelimeter(char ch, const char* delimeter)
{
	char* ptr=(char*)delimeter;
	/*
	if (!isalnum(ch))
	{
		return true;
	}
	*/
	if (ch==10||ch==13)
	{
		return true;
	}
	while (*ptr!='\0')
	{
		if (ch==*ptr)
		{
			return true;
		}
		ptr++;
	}
	return false;</pre>
  <pre>}

bool getWord(FILE* stream, char* buffer, const char* delimeter)
{
	bool isDelimeter(char ch, const char* delimeter);
	char ch;
	bool begin=false, result=false;
	int counter=0;
	while (!feof(stream))
	{
		ch=getc(stream);
		if (!begin)
		{
			//skip white space
			if (isDelimeter(ch, delimeter))
			{
				continue;
			}
			else
			{
				begin=true;
			}
		}

		if (begin)
		{
			if (isDelimeter(ch, delimeter))
			{
				buffer[counter]='\0';
				if (counter!=0)
				{
					result= true;
				}
				break;
			}
			buffer[counter++]=ch;
		}
	}
	if (counter!=0)
	{
		result= true;
	}
	return result;
}

</pre>
</div>
<pre></pre>
<pre></pre>
<pre></pre>
<pre><b><font color="#0000FF"><span lang="en-ca">The result is like following:</span></font></b></pre>

<pre>D:\PROGRA~1\MICROS~2\MYPROJ~1\replace\Debug&gt;replace nick.txt from replaceform
replace string 'from' in 'ion reads data from gavenewstring stand'
(y/n) &gt; y

D:\PROGRA~1\MICROS~2\MYPROJ~1\replace\Debug&gt;</pre>

<pre><span lang="en-ca">			</span></pre>

<pre><span lang="en-ca">				</span> <a href="game24.htm"><img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp;</p>

</body>

</html>