| | |
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:
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
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:
c Syntax (Toggle Plain Text)
input = fopen(argv[3], "r"); output = fopen(argv[4], "w"); flag = 0; while(flag==0) { for(i=0;i<MAX;i++) { if((c = getc(input)) != EOF) { file[i] = c; } else { file[i] = -1; break; } } encrypt(key,file,cipher); for(i=0;i<MAX;i++) { if(cipher[i]!=-1) { fprintf(output,"%c",cipher[i]); } else { fprintf(output,"%c",cipher[i]); i = MAX; flag = 1; //this a flag that exits a while loop not included in this code. } } } fclose(input); fclose(output);
The 'encrypt' function looks like this:
c Syntax (Toggle Plain Text)
void encrypt(char *key,char *plainTxt,char *cipherTxt) { int i,vig[MAX][MAX]; char a,b; createVig(vig); // Creates a standard Vig 128 x 128 table for(i=0;i<MAX;i++) // Loops for 128 characters { a = key[i]; // Assigns the value of key[i] to a b = plainTxt[i]; if(b!=-1) { cipherTxt[i] = vig[a][b]; // Finds the new cipherTxt character and assigns it to cipherTxt[i] } else { cipherTxt[i]=-1; i = MAX; } } }
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
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.
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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.
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.
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.
c Syntax (Toggle Plain Text)
while(fread(file,128,1, input)) { encrypt(key,file,cipher); //function that encrypts 128 bits at a time decrypt(key,plain,cipher);//function that decrypts128 bits at a time fwrite(plain, 128, 1,output); }
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.
Try something like this after the loop:
c Syntax (Toggle Plain Text)
printf("current position = %d\n", curr = ftell(file)); fseek(file, SEEK_END, 1); /* go to end of file */ printf("last position = %d\n", last = ftell(file)); printf("left = %d\n", last - curr);
Last edited by andor; Nov 29th, 2006 at 11:37 am.
If you want to win, you must not loose (Alan Ford)
![]() |
Similar Threads
- save file names into char array (C)
- Reading file input into an array (C++)
- Storing file data in an array? (C++)
- Saving to a file (C)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- Saving a file using C++ (C++)
Other Threads in the C Forum
- Previous Thread: Shortest Job First Algorithm
- Next Thread: How to populate an array? from a FILE
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






