How to re-use the replace() function to replace different strings in the same file ?
-
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
-
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
Your file names are constant strings, defined inside your function. That is not the way to do it. I see basically two ways that could work for you: 1. perform all replace operations in memory, not in files. That way you can load a text file, modify the text as much as you want, then store the final text in the same or in another file. 2. make the source and destination file names input parameters to your function, and use a new destination file name each time you call replace(). :) PS: why are you rewinding the files before closing them?
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
-
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
Faez Shingeri wrote:
rewind(fp1); rewind(fp2);
These are unnecessary.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Faez Shingeri wrote:
rewind(fp1); rewind(fp2);
These are unnecessary.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
I thought rewind will help me during the reuse of the file.. :~ 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
-
I thought rewind will help me during the reuse of the file.. :~ 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
Faez Shingeri wrote:
I thought rewind will help me during the reuse of the file.. :~
But you are not reusing the file. It is being closed. When
replace()
is called again, a whole new file pointer is generated. The fact that it is the same filename is irelevant.Faez Shingeri wrote:
while ((find_ptr = strstr(buff_ptr,text2find)) && find_ptr+find_len+1 == NULL)
This is an accident waiting to happen. Provide a sample of your input file, what string(s) you want replaced, and what string(s) you do not want replaced.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Faez Shingeri wrote:
I thought rewind will help me during the reuse of the file.. :~
But you are not reusing the file. It is being closed. When
replace()
is called again, a whole new file pointer is generated. The fact that it is the same filename is irelevant.Faez Shingeri wrote:
while ((find_ptr = strstr(buff_ptr,text2find)) && find_ptr+find_len+1 == NULL)
This is an accident waiting to happen. Provide a sample of your input file, what string(s) you want replaced, and what string(s) you do not want replaced.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
Sample File to be parsed and replace different strings..
*************************************************************************
* API TEMPLATE PROGRAM *
* THIS PROGRAM SERVES AS A TEMPLATE TO GENERATE THE ACTUAL API PROGRAM *
* *
* MAPPING RULES: *
* -------------------------------------------------------------------- *
* Variable Name Replace with *
* -------------------------------------------------------------------- *
* &apiname API Name from Mapping Repository *
* &dclgen DCLGEN name from Mapping Repository *
* &keyinfo Key field column name from file layout *
* &hostvarprimary Key field host var name from DCLGEN *
* &tablecols Table column name from DCLGEN *
* &tablehostvars Host variable names from DCLGEN *
* &tblprimary Key field column name from DCLGEN *
* &tablehostvars01 01 level of host var from DCLGEN *
*************************************************************************
/* strings to be replaced if present in the above block should be ignored */
IDENTIFICATION DIVISION.
PROGRAM-ID. &apiname. /* &apiname string shud be replaced with another string */EXEC SQL INCLUDE &dclgen END-EXEC. EXEC SQL INCLUDE SQLCA END-EXEC. 2100-SELECT-PARA. MOVE &keyinfo TO &hostvarprimary EXEC SQL SELECT &tablecols /\* &tablecols to be replaced with another string \*/ INTO &tablehostvars /\*&tablehostvars to be replaced with another string \*/ FROM &tblname WHERE &tblprimary = &hostvarprimary END-EXEC IF SQL-CODE EQUAL ZERO SET FUNC-SUCCESS TO TRUE MOVE &tablehostvars01 TO VSAM-REC-BLOCK ELSE PERFORM 9999-ABEND-PARA END-IF. 2100-SELECT-PARA-EXIT. EXIT. 2200-INSERT-PARA. MOVE VSAM-REC-BLOCK TO &tablehostvars01 /\* This &tablehostvars01 string SHOULD NOT be replaced.. \*/ EXEC SQL INSERT INTO &tblname ( &tablecols ) VALUES ( &tablehostvars ) END-EXEC IF SQL-CODE EQUAL ZERO SET FUNC-SUCCESS TO TRUE ELSE PERFORM 9999-ABEND-PARA END-IF. 2200-INSERT-PARA-EXIT. EXIT. 2300-UPDATE-PARA. MOVE VSAM-REC-BLOCK TO &tablehostvars01 /\* shud not be replaced \*/ EXEC SQL
-
Sample File to be parsed and replace different strings..
*************************************************************************
* API TEMPLATE PROGRAM *
* THIS PROGRAM SERVES AS A TEMPLATE TO GENERATE THE ACTUAL API PROGRAM *
* *
* MAPPING RULES: *
* -------------------------------------------------------------------- *
* Variable Name Replace with *
* -------------------------------------------------------------------- *
* &apiname API Name from Mapping Repository *
* &dclgen DCLGEN name from Mapping Repository *
* &keyinfo Key field column name from file layout *
* &hostvarprimary Key field host var name from DCLGEN *
* &tablecols Table column name from DCLGEN *
* &tablehostvars Host variable names from DCLGEN *
* &tblprimary Key field column name from DCLGEN *
* &tablehostvars01 01 level of host var from DCLGEN *
*************************************************************************
/* strings to be replaced if present in the above block should be ignored */
IDENTIFICATION DIVISION.
PROGRAM-ID. &apiname. /* &apiname string shud be replaced with another string */EXEC SQL INCLUDE &dclgen END-EXEC. EXEC SQL INCLUDE SQLCA END-EXEC. 2100-SELECT-PARA. MOVE &keyinfo TO &hostvarprimary EXEC SQL SELECT &tablecols /\* &tablecols to be replaced with another string \*/ INTO &tablehostvars /\*&tablehostvars to be replaced with another string \*/ FROM &tblname WHERE &tblprimary = &hostvarprimary END-EXEC IF SQL-CODE EQUAL ZERO SET FUNC-SUCCESS TO TRUE MOVE &tablehostvars01 TO VSAM-REC-BLOCK ELSE PERFORM 9999-ABEND-PARA END-IF. 2100-SELECT-PARA-EXIT. EXIT. 2200-INSERT-PARA. MOVE VSAM-REC-BLOCK TO &tablehostvars01 /\* This &tablehostvars01 string SHOULD NOT be replaced.. \*/ EXEC SQL INSERT INTO &tblname ( &tablecols ) VALUES ( &tablehostvars ) END-EXEC IF SQL-CODE EQUAL ZERO SET FUNC-SUCCESS TO TRUE ELSE PERFORM 9999-ABEND-PARA END-IF. 2200-INSERT-PARA-EXIT. EXIT. 2300-UPDATE-PARA. MOVE VSAM-REC-BLOCK TO &tablehostvars01 /\* shud not be replaced \*/ EXEC SQL
Can you search for "tablehostvars " (note the space) instead of "tablehostvars"?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous