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.