DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Saving to a file from an array in C ? (http://www.daniweb.com/forums/thread63082.html)

Goldfish691 Nov 28th, 2006 3:03 pm
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:

 
            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:

 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

iamthwee Nov 29th, 2006 2:57 am
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.

WaltP Nov 29th, 2006 3:24 am
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.

Goldfish691 Nov 29th, 2006 8:56 am
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

andor Nov 29th, 2006 9:43 am
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.

Goldfish691 Nov 29th, 2006 11:14 am
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))
                {
                     
                      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.

andor Nov 29th, 2006 11:35 am
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));
fseek(file, SEEK_END, 1); /* go to end of file */
printf("last position = %d\n", last = ftell(file));
printf("left = %d\n", last - curr);

Goldfish691 Nov 29th, 2006 12:40 pm
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