Search and replace program

I need to make a program that searches a file for every occurrence of a user determined word, and replace it with a new one. I wrote the following code:



However, every time I run it, although "./aux.dat" keeps the content of the original file, nothing gets written to the original file. Any ideas?

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
#include <iostream>
#include <fstream>
#include <string.h>
#include <ctype.h>
using namespace std;

int main() {
	char temp, tempaux[30], word[30], word2[30], filename[30];
	cout << "Nome do ficheiro? - "; cin.getline(filename, 30, '\n');
	cout << "Palavra a substituir? - "; cin.getline(word, 30, '\n');
	cout << "Palavra substituta? - "; cin.getline(word2, 30, '\n');
	fstream original(filename, ios::in);
	fstream tempfile("./aux.dat", ios::in|ios::out|ios::trunc);
	{
		if (!(original||tempfile)){
			cout << "Error!!!\nAborting..." << endl;
			return 0;
		}
		
		for (temp = (char) original.get(); temp != EOF; temp = (char) original.get())
			tempfile.put(temp);
		
		original.close();
		fstream out(filename, ios::out|ios::trunc);{
			for (temp = (char) tempfile.get(); temp != EOF; temp = (char) tempfile.get()) {
				if(isalpha(temp)){
					for (int aux = 0 ; isalpha(temp); temp = (char) tempfile.get(), ++aux) {
						tempaux[aux] = temp;
					}
					if (!strcmp(tempaux, word)) {
						for (int aux2 = 0; aux2 <= strlen(word2); ++aux2)
							out.put(word2[aux2]);
					}
					else {
						for (int aux2 = 0; aux2 <= strlen(tempaux); ++aux2)
							out.put(tempaux[aux2]);
					}
				}
				else {
					out.put(temp);
				}
			}
		}
	}
}



However, every time I run it, although "./aux.dat" keeps the content of the original file, nothing gets written to the original file. Any ideas?
At first, I thought the problem was the filename - on a Windows system, the filename "aux.dat" isn't possible - for reasons dating back to the history in DOS. So for my own purposes I used "faux.dat" instead.

But that did highlight the first problem. At line 15,
 
    if (!(original||tempfile)){

what is intended is
if (!original || !tempfile) {

When combining those two conditions, it becomes
 
    if (!(original && tempfile)){

Note the use of && rather than || above.


With that preamble out of the way, now we're ready to answer the original question,
However, every time I run it, although "./aux.dat" keeps the content of the original file, nothing gets written to the original file.

This is quite simple. After writing each character from the original to the copy (tempfile) the file pointer for read/write is positioned at the very end of tempfile. In order to start reading from the beginning, reset the read pointer, using seekg()
 
    tempfile.seekg(0);   // reset read pointer to start of file 


That's it - that answers the question.

http://www.cplusplus.com/reference/istream/istream/seekg/

Still, while I'm here, it's as well to mention a few other things. This seems excessively complex for a simple action like reading each character from a file
1
2
    for (temp = (char) original.get(); temp != EOF; temp = (char) original.get())
        tempfile.put(temp);


Instead I highly recommend this form:
1
2
    while  (original.get(temp))
        tempfile.put(temp);


The same suggestion applies elsewhere in the program, get(temp) is cleaner and arguably easier to use than temp = (char) tempfile.get()

http://www.cplusplus.com/reference/istream/istream/get/

There are a number of other bugs in the code, but I'll leave those for now - part of the fun of coding is to uncover and correct errors.
Thank you, now I see it! And you're probably right about it being overly complex, it's just a natural tendency!
Topic archived. No new replies allowed.