allstar
allstar

Reputation: 109

Filing in C Programming

I am trying to make simple encryption algorithm .My aim is to translate abc (it can be any word) to 123. Then apply some functions then again translate to text, Here is my algorithm .I have problem about filing.I create unencrypted.txt which is written inside "hello world" then debug program it s creating encrypted.txt but just written w o r l d.why it s not writing hello world.always takes last word and with spaces "w o r l d",Can you help me?

Edit https://www.dropbox.com/s/i3afkm439iv3d0v/last%20form.txt this is the last form i added multply 2 and mod 27 for encryption.it works better.but still problem.there is "hello world" in txt file.its encryption pjxxcpjxxcpjxxcpjxxcscixhpjxxcqscixhqpjxxc scixh but it must be pjxxc scixh

   #include <cstdlib>
    #include <iostream>
    #include<stdio.h>
    #include<string.h>
    #include <conio.h>



    int main()
    {   
        int g,p,sak,v,r,t,z,o,c,l,i,j,k,*D;


    char alfabe[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'}; 
     FILE *fp1;
     FILE *mat;
       char word[20];

       fp1 = fopen("unencrypted.txt","r");
       do {
          g = fscanf(fp1,"%s",word); /* dosyadan bir kelime oku... */
          if (g != EOF) {
                mat=fopen("encrypted.txt","w") ;


        c=strlen(word);
        printf("has %d letters ", c);
        D = (int *) malloc( sizeof(int)*c ); 
        for(i=0;i<c;i++) {
            for(j=0;j<26;j++) {
                if(word[i]==alfabe[j]) {  
                     D[i]=(j+1);

                     break;
                }
            }
        }




         printf("\nlast form before translation ");
         for(l=0;l<c;l++) {
           printf("%d",D[l]);  /*it s just for control */

        }
        for(z=0;z<c;z++){

                          o=D[z];
                          word[z]=alfabe[o-1] ;  }   





        printf("\nnew form of word: ");
        for(k=0;k<c;k++) {

           fprintf(mat," %c",word[k]);

        }
    fclose(mat);
    }


    } while (g != EOF);          
       fclose(fp1);   }

Upvotes: 1

Views: 220

Answers (4)

Šimon T&#243;th
Šimon T&#243;th

Reputation: 36451

you are using fscanf(...,"%s",...);

Now that is wrong for a simple reason, no matter how big your buffer is, the input can always be one byte longer.

Since you are processing the input one character at a time, you should read one character at a time.

crazy loop

I'm pretty sure you meant to write D[i] = word[i]-'a';, plus you should test if the character is small alphabetic character by using islower().

o=D[z]; word[z] = alfabe[o-1];

Now this doesn't make much sense. What if the character is a? You would be accessing alfabe[-1].

Upvotes: 0

codaddict
codaddict

Reputation: 455282

You are opening the file for writing inside the loop:

do {
    ...
    mat=fopen("encrypted.txt","w") ;
    ...
    // writing to file
    ...
    } while(g != EOF);

as a result each iteration of the loop will wipe off the old file contents.

Upvotes: 0

P.P
P.P

Reputation: 121407

why it s not writing hello world?

You open the file everytime in the do-while loop:

            mat=fopen("encrypted.txt","w") ;

So everytime the contents are overwritten. As a result, you'll only have the last word written into it. Hence, "hello" disappears.

but just written w o r l d

Because, you use white-space in fprintf:

   fprintf(mat," %c",word[k]);

To fix it:

  1. Open the file only once.
  2. Remove the the white-space from the fprintf. fprintf(mat,"%c",word[k]);

Upvotes: 2

Ovais Khatri
Ovais Khatri

Reputation: 3211

It is doing so because you are open file in "w" write mode, so it clears the previous content. Try opening it in "a+" mode.

Upvotes: 0

Related Questions