944,048 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2080
  • C RSS
Nov 28th, 2006
0

Saving to a file from an array in C ?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Goldfish691 is offline Offline
13 posts
since Apr 2005
Nov 29th, 2006
0

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 29th, 2006
0

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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Nov 29th, 2006
0

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Goldfish691 is offline Offline
13 posts
since Apr 2005
Nov 29th, 2006
0

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

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.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Nov 29th, 2006
0

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.

  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Goldfish691 is offline Offline
13 posts
since Apr 2005
Nov 29th, 2006
0

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

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.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Nov 29th, 2006
0

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

Thanks Andor and everybody else..... got it working now.. much appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Goldfish691 is offline Offline
13 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: What is the use of accessor??
Next Thread in C Forum Timeline: How to populate an array? from a FILE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC