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); }
You are opening the file for writing inside the loop:
as a result each iteration of the loop will wipe off the old file contents.