-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindReplace.cpp
More file actions
119 lines (92 loc) · 2.78 KB
/
findReplace.cpp
File metadata and controls
119 lines (92 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include "findReplace.h"
#include "openFile.h"
#define BUFFER_SIZE 100
/* Function declaration */
void findAndReplaceFile(){
system("cls");
/* File pointer to hold reference of input file */
FILE * fPtr;
FILE * fTemp;
char path[100];
char buffer[BUFFER_SIZE];
char oldWord[100], newWord[100];
printf("Enter path of source file: ");
scanf("%s", path);
/* Open all required files */
fPtr = fopen(path, "r");
fTemp = fopen("replace.txt", "w");
/* fopen() return NULL if unable to open file in given mode. */
if (fPtr == NULL || fTemp == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open file.\n");
printf("Please check whether file exists and you have read/write privilege.\n");
exit(EXIT_SUCCESS);
}
printf("\n");
/* print text sebelum */
printFromFile(path);
printf("\n\nEnter word to replace: ");
scanf("%s", oldWord);
printf("Replace '%s' with: ", oldWord);
scanf("%s", newWord);
/*
* Read line from source file and write to destination
* file after replacing given word.
*/
while ((fgets(buffer, BUFFER_SIZE, fPtr)) != NULL)
{
// Replace all occurrence of word from current line
findReplace(buffer, oldWord, newWord);
// After replacing write it to temp file.
fputs(buffer, fTemp);
}
/* Close all files to release resource */
fclose(fPtr);
fclose(fTemp);
/* Delete original source file */
remove(path);
/* Rename temp file as original file */
rename("replace.txt", path);
// print sesudah di find replaca
printFromFile(path);
printf("\nSuccessfully replaced all occurrences of '%s' with '%s'.\n", oldWord, newWord);
system("Pause");
//print text sesudah
}
/**
* Replace all occurrences of a given a word in string.
*/
void findReplace(char *str, const char *oldWord, const char *newWord)
{
char *pos, temp[BUFFER_SIZE];
int index = 0;
int owlen;
owlen = strlen(oldWord);
// Fix: If oldWord and newWord are same it goes to infinite loop
if (!strcmp(oldWord, newWord)) {
return;
}
/*
* Repeat till all occurrences are replaced.
*/
while ((pos = strstr(str, oldWord)) != NULL)
{
// Backup current line
strcpy(temp, str);
// Index of current found word
index = pos - str;
// Terminate str after word found index
str[index] = '\0';
// Concatenate str with new word
strcat(str, newWord);
// Concatenate str with remaining words after
// oldword found index.
strcat(str, temp + index + owlen);
}
}