Saving to a file from an array in C ?

Reply

Join Date: Apr 2005
Posts: 13
Reputation: Goldfish691 is an unknown quantity at this point 
Solved Threads: 0
Goldfish691's Avatar
Goldfish691 Goldfish691 is offline Offline
Newbie Poster

Saving to a file from an array in C ?

 
0
  #1
Nov 28th, 2006
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:

  1.  
  2. input = fopen(argv[3], "r");
  3. output = fopen(argv[4], "w");
  4.  
  5. flag = 0;
  6.  
  7. while(flag==0)
  8. {
  9. for(i=0;i<MAX;i++)
  10. {
  11. if((c = getc(input)) != EOF)
  12. {
  13. file[i] = c;
  14. }
  15. else
  16. {
  17. file[i] = -1;
  18. break;
  19. }
  20.  
  21. }
  22.  
  23. encrypt(key,file,cipher);
  24.  
  25. for(i=0;i<MAX;i++)
  26. {
  27. if(cipher[i]!=-1)
  28. {
  29. fprintf(output,"%c",cipher[i]);
  30. }
  31. else
  32. {
  33. fprintf(output,"%c",cipher[i]);
  34. i = MAX;
  35. flag = 1; //this a flag that exits a while loop not included in this code.
  36. }
  37. }
  38. }
  39. fclose(input);
  40. fclose(output);

The 'encrypt' function looks like this:

  1. void encrypt(char *key,char *plainTxt,char *cipherTxt)
  2. {
  3. int i,vig[MAX][MAX];
  4. char a,b;
  5.  
  6. createVig(vig); // Creates a standard Vig 128 x 128 table
  7.  
  8. for(i=0;i<MAX;i++) // Loops for 128 characters
  9. {
  10. a = key[i]; // Assigns the value of key[i] to a
  11. b = plainTxt[i];
  12.  
  13. if(b!=-1)
  14. {
  15. cipherTxt[i] = vig[a][b]; // Finds the new cipherTxt character and assigns it to cipherTxt[i]
  16. }
  17. else
  18. {
  19. cipherTxt[i]=-1;
  20. i = MAX;
  21. }
  22. }
  23. }

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
Free entry to win £1000

http://www.win1000free.co.uk
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Saving to a file from an array in C ?

 
0
  #2
Nov 29th, 2006
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Saving to a file from an array in C ?

 
0
  #3
Nov 29th, 2006
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: Goldfish691 is an unknown quantity at this point 
Solved Threads: 0
Goldfish691's Avatar
Goldfish691 Goldfish691 is offline Offline
Newbie Poster

Re: Saving to a file from an array in C ?

 
0
  #4
Nov 29th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Saving to a file from an array in C ?

 
0
  #5
Nov 29th, 2006
First of all you open the file in binary mode:

input = fopen(argv[3], "rb");
output = fopen(argv[4], "wb");

and then using fwrite function.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: Goldfish691 is an unknown quantity at this point 
Solved Threads: 0
Goldfish691's Avatar
Goldfish691 Goldfish691 is offline Offline
Newbie Poster

Re: Saving to a file from an array in C ?

 
0
  #6
Nov 29th, 2006
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.

  1. while(fread(file,128,1, input))
  2. {
  3.  
  4. encrypt(key,file,cipher); //function that encrypts 128 bits at a time
  5.  
  6. decrypt(key,plain,cipher);//function that decrypts128 bits at a time
  7.  
  8. fwrite(plain, 128, 1,output);
  9.  
  10. }

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.
Free entry to win £1000

http://www.win1000free.co.uk
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Saving to a file from an array in C ?

 
0
  #7
Nov 29th, 2006
Try something like this after the loop:
  1. printf("current position = %d\n", curr = ftell(file));
  2. fseek(file, SEEK_END, 1); /* go to end of file */
  3. printf("last position = %d\n", last = ftell(file));
  4. 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)
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 13
Reputation: Goldfish691 is an unknown quantity at this point 
Solved Threads: 0
Goldfish691's Avatar
Goldfish691 Goldfish691 is offline Offline
Newbie Poster

Re: Saving to a file from an array in C ?

 
0
  #8
Nov 29th, 2006
Thanks Andor and everybody else..... got it working now.. much appreciated.
Free entry to win £1000

http://www.win1000free.co.uk
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC