m!ckey
March 28th, 2006, 07:21 AM
Hi,
I was trying out the following code to search and replace words in 'n' number of files but nothing is being written into the new files! Can anybody tell me why???
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <dir.h>
FILE *fptr2;
void replace(const char *src, const char *from, const char *to)
{
size_t size = strlen(src) + 1;
size_t fromlen = strlen(from);
size_t tolen = strlen(to);
char *value = malloc(size);
char *dst = value;
if ( value != NULL )
{
for ( ;; )
{
const char *match = strstr(src, from);
if ( match != NULL )
{
size_t count = match - src;
char *temp;
size += tolen - fromlen;
temp = realloc(value, size);
if ( temp == NULL )
{
free(value);
}
dst = temp + (dst - value);
value = temp;
memmove(dst, src, count);
src += count;
dst += count;
memmove(dst, to, tolen);
src += fromlen;
dst += tolen;
}
else
{
strcpy(dst, src);
break;
}
}
}
fprintf(fptr2,"%s",value,strlen(value));
}
int main(void)
{
FILE *fptr1;
char s[300];
const char *from,*to;
char *fname;
char target[100];
char *p=target;
char *add="NEW_";
int count=0;
struct ffblk f;
int done = findfirst("*.csl",&f,1);
clrscr();
from="CTV";
to="cute";
while (!done)
{
if (done) break;
fname=f.ff_name;
printf("\nfname=%s",fname);
done = findnext(&f);
if (strstr(fname,"NEW_")==NULL)
{
count++;
fptr1=fopen(fname,"r+");
if (fptr1==NULL) printf("\nSource Error");
else printf("\nSource opened\n");
target[0]='\0';
p=strcat(p,add);
p=strcat(p,fname);
printf("\ntarget=%s",target);
fptr2=fopen(target,"w+");
if (fptr2==NULL) printf("\nTarget Error");
else printf("\nTarget opened\n");
while (fgets(s,300,fptr1)!=NULL)
{
replace(s, from, to);
}
fclose(fptr2);
fclose(fptr1);
}
else continue;
}
printf("\ncount=%d",count);
getch();
return 0;
}
I was trying out the following code to search and replace words in 'n' number of files but nothing is being written into the new files! Can anybody tell me why???
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <dir.h>
FILE *fptr2;
void replace(const char *src, const char *from, const char *to)
{
size_t size = strlen(src) + 1;
size_t fromlen = strlen(from);
size_t tolen = strlen(to);
char *value = malloc(size);
char *dst = value;
if ( value != NULL )
{
for ( ;; )
{
const char *match = strstr(src, from);
if ( match != NULL )
{
size_t count = match - src;
char *temp;
size += tolen - fromlen;
temp = realloc(value, size);
if ( temp == NULL )
{
free(value);
}
dst = temp + (dst - value);
value = temp;
memmove(dst, src, count);
src += count;
dst += count;
memmove(dst, to, tolen);
src += fromlen;
dst += tolen;
}
else
{
strcpy(dst, src);
break;
}
}
}
fprintf(fptr2,"%s",value,strlen(value));
}
int main(void)
{
FILE *fptr1;
char s[300];
const char *from,*to;
char *fname;
char target[100];
char *p=target;
char *add="NEW_";
int count=0;
struct ffblk f;
int done = findfirst("*.csl",&f,1);
clrscr();
from="CTV";
to="cute";
while (!done)
{
if (done) break;
fname=f.ff_name;
printf("\nfname=%s",fname);
done = findnext(&f);
if (strstr(fname,"NEW_")==NULL)
{
count++;
fptr1=fopen(fname,"r+");
if (fptr1==NULL) printf("\nSource Error");
else printf("\nSource opened\n");
target[0]='\0';
p=strcat(p,add);
p=strcat(p,fname);
printf("\ntarget=%s",target);
fptr2=fopen(target,"w+");
if (fptr2==NULL) printf("\nTarget Error");
else printf("\nTarget opened\n");
while (fgets(s,300,fptr1)!=NULL)
{
replace(s, from, to);
}
fclose(fptr2);
fclose(fptr1);
}
else continue;
}
printf("\ncount=%d",count);
getch();
return 0;
}