Originally Posted by
Prelude
>You might also want to make sure you close the file properly.
It certainly couldn't hurt if you
really need robust code or if you don't flush the stream before closing it and a write error occurs. At that point you're looking at a loss of data situation, and recovery is critical. For toy programs like we write here, you just need to think of a call to fclose as a placeholder for something more solid.
can't help it you guys are turning me into a pedant
So as to the problem the op is having. I've been playing around and came up with some code. It seemed to have worked. So in trully annoying style i'll just post the logic here. You guys can let me know if you see anything wrong with my pseucode.
Code:
begin:
type_t encrypt(int) //whatever type your return value is
FILE *file1,*file2;
int ltr,code;
open file1 for reading:
do error handling
open file2 for writing:
do error handling
while ( (ltr =fgetc(file1)) !=EOF):
code = encrypt(ltr)
fputc(code,file2)
if (both file1 && file2 are not close):
give error message
remove file1
end
The basic idea is to have two files open at the same time. Seems depending on the OS you can have 10 to about 20 files open at the same time.
one file is only for reading which contains the original message, the other is for writing.
you might want to make a function to encrypt your data at least for modularity purposes.
once you copied the changed data to the second file, close both files like so
Code:
if (fclose(file) != 0 || fclose(file2) != 0)
fprintf(stderr,"Error in closing files\n");
and then remove the first file with the original message. Which is obviously not really deleted, simply removed the link to it.
Wish i knew more about files right now, but Just started. let me know how it goes. I've done it alright. I'm sure there is an easier way. Like Dave_sinkula's but i don't know that yet.