   Can't reopen file with std::ifstream
  I'm attempting to write a simple XOR-based encryption program, and the encryption phase works out just fine. However, when I try to decrypt the file, the decrypted file doesn't even exist. To figure out what's going on, I made a simpler program that merely copied a file, char by char, to a new file. Then I rewrote the decryption program to do the same thing. This is the code for the first program; the second program is identical except that it reads from the .pak file and writes to "stuff2.txt".#include <fstream>int main(){  std::ifstream txtfile("stuff.txt");  std::ofstream pakfile("stuff.pak");  char c;  while (txtfile.get(c))  {    pakfile.put(c);  }  return 0;}After the first program, "stuff.pak" is identical to "stuff.txt", as expected. The second program does absolutely nothing; "stuff2.txt" was never created. I even put a counter in the loop to count how many characters get read, and it returns 0 characters. What am I missing?   
  I can't say for sure without the source for the second program, but I think you might have mixed up the variable declarations. I think this is right:#include <fstream>int main(){  std::ifstream pakfile("stuff.pak");  std::ofstream txtfile("stuff2.txt");  char c;  while (pakfile.get(c))  {    txtfile.put(c);  }  return 0;}EDIT: also, make sure you re-run the first program before trying this code. If the second program accidentally opened the .pak file as an ofstream, it will have truncated the .pak file to 0 bytes.   ;  You should close() the ofstream when you're done with it. Otherwise it may not save anything in the file and therefore you end up reading 0 bytes from it later.   ;  That is indeed the exact source code for the second program. Hence my confusion. :)   ;Quote:Original post by MindFlayerYou should close() the ofstream when you're done with it. Otherwise it may not save anything in the file and therefore you end up reading 0 bytes from it later.fstreams are automatically closed at the end of the scope (the main() function in this case).Quote:Original post by RainaultThat is indeed the exact source code for the second program. Hence my confusion. :)Perhaps it's not opening the .pak file. Have you tried calling is_open() on the input stream?   ;  Hm... well, that's enlightening. You're right; the .pak file isn't opening at all in the second program. And I did rerun the first program before each time I ran the second. What would cause the file to fail to open? I can open it in Notepad and see the contents just fine.EDIT: Open mouth and insert foot. This has got to be the most ridiculous mistake I've ever made. The .pak file is in the folder for the first program, so of course it doesn't exist in the folder of the second program.I really shouldn't be programming this late at night.   ;  D'oh! And that's what I get for being afraid to suggest that possibility. Good luck with your program. :-)   ;  And don't forget - xor encryption is not secure. Might as well encrypt in pig latin!Cheers.   