D
DFS
* reads an existing file
* writes changes to new file
* counts replacements made by line
* counts total replacements made
* no fancy usage of sed!
I KNOW someone can better my piddly effort below (actually one I found
online and made mods to):
=================================================================================
#include <stdio.h>
#include <string.h>
int findreplace(void)
{
int bufferSize = 0x1000;
int i = 0, k = 0, j = 0;
char buffer[bufferSize];
FILE *inFile = fopen("random_in.txt", "rt");
FILE *outFile = fopen("random_out.txt", "w+");
char *find = "46";
char *replace = "----";
if(inFile == NULL || outFile == NULL)
{
printf("Error opening file(s)");
return 1;
}
printf("Replace '%s' with '%s':\n", find, replace);
while(fgets(buffer, bufferSize, inFile) != NULL)
{
char *stop = NULL;
char *start = buffer;
k = 0;
while(1)
{
stop = strstr(start, find);
if(stop == NULL)
{
fwrite(start, 1, strlen(start), outFile);
break;
} else {
fwrite(start, 1, stop - start, outFile);
fwrite(replace, 1, strlen(replace), outFile);
start = stop + strlen(find);
k++;
}
}
i++;
j += k;
printf("Line %d: %d replacements made\n", i, k);
}
printf("%d replacements made.\n", j);
fclose(inFile);
fclose(outFile);
return 0;
}
int main(void) {
findreplace();
return 0;
}
=================================================================================
input (random_in.txt)
14513111664214260256543011122553234523520226455552
41602561064325541006060354620223361346535061545034
63164621623130051346620535103421535300201464252314
30013144611120401561305220534605456101542562311260
30501506124251042546364005110661421500320026101445
35355334213621124600100142264440253516210400362562
65140560414014522562466550406113020500531011441421
60543325410345553336424511333322104440166124450061
44310321435636412163052026304311532342515351020026
10536502643531635353214012163164121056142415600245
output (random_out.txt)
14513111664214260256543011122553234523520226455552
4160256106432554100606035----202233613----535061545034
6316----216231300513----620535103421535300201----4252314
3001314----1112040156130522053----05456101542562311260
305015061242510425----364005110661421500320026101445
3535533421362112----00100142264440253516210400362562
65140560414014522562----6550406113020500531011441421
60543325410345553336424511333322104440166124450061
44310321435636412163052026304311532342515351020026
10536502643531635353214012163164121056142415600245
=================================================================================
[dfs@home files]$ ./find_replace
Replace '46' with '----':
Line 1: 0 replacements made
Line 2: 2 replacements made
Line 3: 3 replacements made
Line 4: 2 replacements made
Line 5: 1 replacements made
Line 6: 1 replacements made
Line 7: 1 replacements made
Line 8: 0 replacements made
Line 9: 0 replacements made
Line 10: 0 replacements made
10 replacements made.
=================================================================================
* writes changes to new file
* counts replacements made by line
* counts total replacements made
* no fancy usage of sed!
I KNOW someone can better my piddly effort below (actually one I found
online and made mods to):
=================================================================================
#include <stdio.h>
#include <string.h>
int findreplace(void)
{
int bufferSize = 0x1000;
int i = 0, k = 0, j = 0;
char buffer[bufferSize];
FILE *inFile = fopen("random_in.txt", "rt");
FILE *outFile = fopen("random_out.txt", "w+");
char *find = "46";
char *replace = "----";
if(inFile == NULL || outFile == NULL)
{
printf("Error opening file(s)");
return 1;
}
printf("Replace '%s' with '%s':\n", find, replace);
while(fgets(buffer, bufferSize, inFile) != NULL)
{
char *stop = NULL;
char *start = buffer;
k = 0;
while(1)
{
stop = strstr(start, find);
if(stop == NULL)
{
fwrite(start, 1, strlen(start), outFile);
break;
} else {
fwrite(start, 1, stop - start, outFile);
fwrite(replace, 1, strlen(replace), outFile);
start = stop + strlen(find);
k++;
}
}
i++;
j += k;
printf("Line %d: %d replacements made\n", i, k);
}
printf("%d replacements made.\n", j);
fclose(inFile);
fclose(outFile);
return 0;
}
int main(void) {
findreplace();
return 0;
}
=================================================================================
input (random_in.txt)
14513111664214260256543011122553234523520226455552
41602561064325541006060354620223361346535061545034
63164621623130051346620535103421535300201464252314
30013144611120401561305220534605456101542562311260
30501506124251042546364005110661421500320026101445
35355334213621124600100142264440253516210400362562
65140560414014522562466550406113020500531011441421
60543325410345553336424511333322104440166124450061
44310321435636412163052026304311532342515351020026
10536502643531635353214012163164121056142415600245
output (random_out.txt)
14513111664214260256543011122553234523520226455552
4160256106432554100606035----202233613----535061545034
6316----216231300513----620535103421535300201----4252314
3001314----1112040156130522053----05456101542562311260
305015061242510425----364005110661421500320026101445
3535533421362112----00100142264440253516210400362562
65140560414014522562----6550406113020500531011441421
60543325410345553336424511333322104440166124450061
44310321435636412163052026304311532342515351020026
10536502643531635353214012163164121056142415600245
=================================================================================
[dfs@home files]$ ./find_replace
Replace '46' with '----':
Line 1: 0 replacements made
Line 2: 2 replacements made
Line 3: 3 replacements made
Line 4: 2 replacements made
Line 5: 1 replacements made
Line 6: 1 replacements made
Line 7: 1 replacements made
Line 8: 0 replacements made
Line 9: 0 replacements made
Line 10: 0 replacements made
10 replacements made.
=================================================================================