954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reuse of replace function multiple times

Hi,
I have a replace function which replaces all the occurences of the string into another file. But when I use the replace function multiple times... it replaces the string which was passed in the last call only and all the previous replacement does not happen.
Below is the code

replace(char text2find[80],char text2repl[80])
 {
 
        char fileOrig[32] = "OrigFile.txt";
 	char fileRepl[32] = "ReplacedFile.txt";
 	   
        char buffer[MAX_LEN_SINGLE_LINE+2];
 	char *buff_ptr, *find_ptr, *tok;
 	FILE *fp1, *fp2;
 	size_t find_len = strlen(text2find);
 
 	fp1 = fopen(fileOrig,"r");
 	fp2 = fopen(fileRepl,"w+");
 

 	while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
 	    {
 	        buff_ptr = buffer;
 	        tok = strtok(buff_ptr,"*");//ignores the string occurence after *
 	      if(tok != NULL)
 	       {
 	        	while ((find_ptr = strstr(buff_ptr,text2find)))
 	        	{
 
 	        		while(buff_ptr < find_ptr)
 	                fputc((int)*buff_ptr++,fp2);
 
 	        	fputs(text2repl,fp2);
 
 	            buff_ptr += find_len;
 
 	        	}
 	        	fputs(buff_ptr,fp2);
 	       }
 	    }
 	    rewind(fp1);
 	    rewind(fp2);
 	    fclose(fp2);
 	    fclose(fp1);
 }

I wud actually wanto replace all the string in the same file, but am finding it tough and also reuse of the replace function multiple times wud be difficult. Hence went with two files...

Thanks,
Faez

faezshingeri
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Maybe you would like to post the code where you have called replace function.

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
int main ()
{
//performing alot of file operations using strings..
//Out of all the loops now... Finally I call the below in main()
replace("&apiname","Buhahahha");
	for (k=0;k<n-1;k++)
	{
		strcat(StrFinCol, tabCol[k]);
		strcat(StrFinCol, ";");
	    k=k+1;
		strcat(StrFinCol, tabCol[k]);
		strcat(StrFinCol, ";");
	}


	for (k=0;k<n-1;k++)
		{
			strcat(StrFinRow, tabRow[k]);
			strcat(StrFinRow, ";");
		    k=k+1;
			strcat(StrFinRow, tabRow[k]);
			strcat(StrFinRow, ";");
		}
	replace1("&tablehostvars",StrFinRow);
	replace1("&tablecols",StrFinCol);

	printf("StrFinCol: %s\n", StrFinCol);
	printf("StrFinRow: %s\n", StrFinRow);




	return 0;
}
faezshingeri
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
replace1("&tablehostvars",StrFinRow);

Is that a different function ?

It will be better if you post the entire code.

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_LEN_SINGLE_LINE     120
#define bufsize 1024
char tabCol [10][20];
char tabRow [10][20];
char StrFinCol [150];
char StrFinRow [150];


 replace(char text2find[80],char text2repl[80])
{

	 char fileOrig[32] = "API Template.txt";
	 char fileRepl[32] = "myReplacedFile.txt";

	   //  const char text2find[80] = "EXEC";
	  //  const char text2repl[80] = "\n";

	    char buffer[MAX_LEN_SINGLE_LINE+2];
	    char *buff_ptr, *find_ptr, *tok;
	    FILE *fp1, *fp2;
	    size_t find_len = strlen(text2find);

	    fp1 = fopen(fileOrig,"r");
	    fp2 = fopen(fileRepl,"w+");


	    while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
	    {
	        buff_ptr = buffer;
	        tok = strtok(buff_ptr,"*");
	        if(tok != NULL)
	        {
	        	while ((find_ptr = strstr(buff_ptr,text2find)))
	        	{

	        		while(buff_ptr < find_ptr)
	                fputc((int)*buff_ptr++,fp2);

	        	fputs(text2repl,fp2);

	            buff_ptr += find_len;

	        	}
	        	fputs(buff_ptr,fp2);
	        }
	    }
	    rewind(fp1);
	    rewind(fp2);

	    fclose(fp2);
	    fclose(fp1);

}

replace1(char text2find[80],char text2repl[80])
 {

 	char fileOrig[32] = "myReplacedFile.txt";
 	char fileRepl[32] = "myFinalFile.txt";
 	   //  const char text2find[80] = "EXEC";
 	  //  const char text2repl[80] = "\n";



 	    char buffer[MAX_LEN_SINGLE_LINE+2];
 	    char *buff_ptr, *find_ptr, *tok;
 	    FILE *fp1, *fp2;
 	    size_t find_len = strlen(text2find);

 	    fp1 = fopen(fileOrig,"r");
 	    fp2 = fopen(fileRepl,"w+");


 	    while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
 	    {
 	        buff_ptr = buffer;
 	        tok = strtok(buff_ptr,"*");
 	      if(tok != NULL)
 	       {
 	        	while ((find_ptr = strstr(buff_ptr,text2find)))
 	        	{

 	        		while(buff_ptr < find_ptr)
 	                fputc((int)*buff_ptr++,fp2);

 	        	fputs(text2repl,fp2);

 	            buff_ptr += find_len;

 	        	}
 	        	fputs(buff_ptr,fp2);
 	       }
 	    }
 	   rewind(fp1);
 	   rewind(fp2);
 	    fclose(fp2);
 	    fclose(fp1);
 }


int main()
{
	char fileOrig[32] = "CANCLT.DCL";
	char fileDupl[32] = "NEWCANCLT.DCL";
	char fileFinal[32] = "FINALCANCLT.DCL";


	char *buf[120], *tok, *tok1;
	char tablCol [10][10];
	int i=0,j=0,k=0,l=0,m=0,n;



	FILE *fp1,*fp2,*fp3;
	fp1=fopen(fileOrig,"r+");
	fp2=fopen(fileDupl, "w+");
	fp3=fopen(fileFinal, "w+");

	while(fgets(buf, bufsize, fp1) != NULL)
	{
		tok = strtok(buf,"  ( ,) *01\n.");
		if (tok != NULL)
		{


			if (i == 0)
			        {
			            // not capturing yet, so check for start of list
			            if (strcmp(tok, "EXEC") == 0)
			                i = 1;
			        }
			else  // bCapture == TRUE
			        {
			            // bCapture is true so check for end of
			            // list, or display token
			            if (strcmp(tok, "END-EXEC") == 0)
			                i = 0;
			            else
			            {
			               	strcpy(tabCol[j],tok);j++;

			          		fprintf(fp2,"Token: %s\n", tok);

			            }
			        }
			if (k == 0)
						        {
						            // not capturing yet, so check for start of list
						            if (strcmp(tok, "END-EXEC") == 0)
						                k = 1;
						        }

			else  // bCapture == TRUE
						        {
						            // bCapture is true so check for end of
						            // list, or display token
						            if (strcmp(tok, "THE") == 0)
						            {
						                  k = 0;
						            }
						            else
						            {
						            	strcpy(tabRow[m],tok);m++;
						              	fprintf(fp3,"Token: %s\n", tok);

						            }
						        }


		}

	}
	j=0,n=0;
	for(j=0;j<=m;j++)
	{
		if(strcmp(tabCol[n],tabRow[j])==0)
		{
			k=j;
			strcpy(tabRow[n],tabRow[k+1]);
			n++;
		}
		else ;


	}
/*	for(k=0;k<n-1;k++)
	{
	printf("No %d Element under TableCol is = %s\n", k+1,tabCol[k]);
	printf("No %d Element under TableRow is = %s\n", k+1,tabRow[k]);
	}
*/

	fclose(fp3);
	fclose(fp2);
	fclose(fp1);

	replace("&apiname","Buhahahha");
	for (k=0;k<n-1;k++)
	{
		strcat(StrFinCol, tabCol[k]);
		strcat(StrFinCol, ";");
	    k=k+1;
		strcat(StrFinCol, tabCol[k]);
		strcat(StrFinCol, ";");
	}


	for (k=0;k<n-1;k++)
		{
			strcat(StrFinRow, tabRow[k]);
			strcat(StrFinRow, ";");
		    k=k+1;
			strcat(StrFinRow, tabRow[k]);
			strcat(StrFinRow, ";");
		}
	replace1("&tablehostvars",StrFinRow);
	replace1("&tablecols",StrFinCol);

	printf("StrFinCol: %s\n", StrFinCol);
	printf("StrFinRow: %s\n", StrFinRow);




	return 0;
}
faezshingeri
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Also,The replace function doesnot check for the entire string

I mean.. it replaces all the strings like &tabhostvars and &tabhostvars01 (I do not want this string to be replaced..) I only want &tabhostvars strings to be replaced

Can I do this..?? #vaguequestionButlearning
while ((find_ptr = strstr(buff_ptr,text2find)) && find_ptr+find_len+1 == NULL)

Thanks,
Faez

faezshingeri
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: