![]() |
| ||
| Saving to a file from an array in C ? Hi there, I'm trying to write a simple encryption program that reads normal text from a txt file - encrypts it using a Vigenère Table - then outputs the results to another file. This process should obviously be reversible as well. I have written this for the encryption part of the program:
The 'encrypt' function looks like this: void encrypt(char *key,char *plainTxt,char *cipherTxt) I decrypt in almost the same way with a few differences. The problem im having is when I decrypt from the cipher text file it only gets about 200 characters in and then finds an EOF (-1) in the text file although there isnt one there. I tested my algorithms by encrypting and decrypting without writing them to a file and they worked fine. I believe this has narrowed it down to how it is written to the file however, I don't know of any other ways in which to do it. I hope that made sense to some one out there. Any help would be greatly appreciated. Many Thanks Craig |
| ||
| Re: Saving to a file from an array in C ? Try reading and writing in binary mode I know from experience, that encryption programs have problems if you're using just .txt files. Reading and writing in binary mode with a .dat file worked for me. |
| ||
| Re: Saving to a file from an array in C ? Since only you know how the encryption works, we can only guess. And my guess is: If your encryption changes a valid character to 0x1A, you get EOF in your file. To solve this, change your encrypted file to a binary file instead of text and that should solve your problem. |
| ||
| Re: Saving to a file from an array in C ? Hiya, Thanks for your replies. I can say that i did try changing the extension to '.dat' however, I didn't realize that you could change the way the file is written. How do I change to writing in binary mode? Regards Craig |
| ||
| Re: Saving to a file from an array in C ? First of all you open the file in binary mode: and then using fwrite function. |
| ||
| Re: Saving to a file from an array in C ? Thanks for all the help... I'm new to the fread and fwrite functions however, i have managed to get most of it working to try and keep it simple for testing i have made this algorithm. while(fread(file,128,1, input)) this works however,cuts about 100 bits of the end. I thought it was because the while loop exits before the last bytes have been written so i added a fwrite outside of the loop but it writes the full 128 bites that were in the array. Is there a way of finding out how many bytes there are left before it reached the EOF marker so that i can set a variable? Or have a gone completely in the wrong direction and there is a super easy way to make this work? thanks again. |
| ||
| Re: Saving to a file from an array in C ? Try something like this after the loop: printf("current position = %d\n", curr = ftell(file)); |
| ||
| Re: Saving to a file from an array in C ? Thanks Andor and everybody else..... got it working now.. much appreciated. |
| All times are GMT -4. The time now is 11:31 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC