Sie sind auf Seite 1von 3

#include <stdio.h> #include <conio.

h> typedef struct key{ //Declare a sturcture in order to store the key s char first; char second; }key; key alp[52]; //As we have 52 lines in key file,we have to tak e an array of sturture of 52 elements to store there keys int read_key(char *); //This function will read the file which contain the keys and will store them in the structure elements void init(void); //This function just initilize the structure eleme nts with Null; void buffer(void); //This function is just for emergency use,some time s it happens to clean the buffer,but this program did not use this function int read_and_replace(char *plane); /*This is the main function,it will read th e input file and at the same time store the modified words in output file until i nput file is not finished */ void init () //Definition of init function { int i; for(i=0; i<=52; i++) { alp[i].first =0; alp[i].second=0; } } void buffer() { char c; do scanf("%c",&c); while(c!='\n'); } int read_key(char *key) { int i=0; char templine[10]; //this string is to store the content of a single line of key file FILE *fkey; fkey=fopen(key,"r"); //open key file for reading if(fkey==NULL) return -1; else { while(!feof(fkey)) { fgets(templine, 10, fkey); sscanf(templine,"%c %c",&alp[i].first,&alp[i].second); /*As we kno w each line contain only two elements,so to store these two eleme nts in the structure*/ i++;

} } return 1; } int read_and_replace(char *plane) //the name of input file is pass through the function,if fucntion works corretly than it will return 1,if not than -1 { int i=0; char temp=0; FILE *fplane; fplane=fopen(plane,"r"); //opening input file for reading if(fplane==NULL) return -1; else { FILE *fout; fout=fopen("output.txt","w"); //if input file open successfully than o pen output file also if(fout==NULL) return -1; while(!feof(fplane)) //Loop will continue until the (End Of File )EOF reached { temp=fgetc(fplane); //take a single character from input file an d store it in temp character if(temp!=0) { if( (temp>=65 && temp<=90) || (temp>=97 && temp<=122) ) //T o check that the character is alphabit(uppercase or lowercase) { for(i=0; i<=52; i++) { if(temp==alp[i].first) //if character is alphabit tha n store its corresponding in output file { fputc(alp[i].second,fout); //For storing correspoin g character in output file } } } else //if character is not alphabit i,e(digit or special charac ter) store as it is in output file without changing { if(temp != EOF) //Do this operation until EOF is not arrive d,it EOF reached than don't store it fputc(temp,fout); } } else return -1; } fclose(fplane); fclose(fout); return 1; } } //After reading completly close input file //After writing completly close output file

main() { init(); int check,check2; char pname[10],kname[10]; //The two strings are to store the nam es of the key file and input file printf("\n Please enter plane file name"); scanf("%s",pname); printf("\n Enter Key file name"); scanf("%s",kname); check2=read_key(kname); if(check2==1) { printf("\n Key file has been read successfully\n"); check=read_and_replace(pname); if(check==1) printf("\n File plane has been successully read and required output file has been created successfully"); else printf("\n An erroe has occured while reading the file plane or creating output or storing characters in output file"); } else printf("\n Unable to read Key file,and thats why no operation can be per formed,Sorry of inconveniance"); getch(); }

Das könnte Ihnen auch gefallen