c - Encoding text and file write read error -


I want to write simple encrypted text system which is read from in.txt, then write out.txt encryption type Every ASCII is plus five if four BTW U and Z, U = A, V = B, ......, Z = F where is my problem? In addition, if we have four BTV U and Z, U = A, V = B, ......, Z = F, where I have a problem, then we use the modulus operator (%) instead of (char-20) can do? My code is below, thanks everyone's answer to the answer. The text is in in.txt, which tests abcde

  #include & lt; Stdio.h & gt; Int main () {char ch; Int cents; File * fptr_in; File * fptr_out; Fptr_in = fopen ("in.txt", "r"); Fptr_out = fopen ("out.txt", "W"); If (fptr_in == NULL || fptr_out == NULL) printf ("File can not be opened \ n"); Else {st = (fscanf (fptr_in, "% c", and ch)); While (cents == 1) for {/ * (st = (fscanf (fptr_in, "% c", and ch)); st == 1; st = (fscanf (fptr_in, "% c", and F))) * / st = (fscanf (fptr_in, "% c", and ch)); If ('A' and Lt; = Seand & amp; amp; & lt; = 'z') {FSCFF (FTPPRIIN, "% C", and CH); If (ch == '') fprintf (fptr_out, "% c", ''); Otherwise if ('U' and LT; = C & amp; amp;; CH & lt; = 'z') fprintf (fptr_out, "% c", (four) ((int) ch-20)); Other fprintf (fptr_out, "% c", (four) ((int) ch + 5)); }}} Fclose (fptr_in); Fclose (fptr_out); Return 0;    

Some comments on your code:

  • It is good that you check the file pointers after calling fopen , but if you can not open them, then you should be out of the program. Otherwise, while the loop has been entered and fscan calls a zero indicator you exit (1) to & lt ; Stdlib.h & gt; . (It's almost like returning from 1 to main , but it can be said from everywhere.)
  • fscanf (f, "% c", & Amp; ch) is better provided as ch = getc (f) . Similarly, fprintf (f, "% c", ch) can be written more precisely as putc (ch, f) . (Not your variants, but in my opinion, useless, verbatim.)
  • There are three cases of your cipher code: space, Jade letter from U, and everything else means that you use punctuation and new Special letters like letters also change. You should probably use uppercase letters, lowercase letters and everything else in three cases.

    Below is an implementation implementation: Things to note:

    • I use the getc for which Ch .
    • Characters are done in an endless loop with <; em> in.txt to (;; ). Loop ends with the break statement when EOF is read. (Ii is usually better to have loops with better position, but in this case I prefer break because it allows me to make ch local for this code And also 'does not need work in situations, which makes me feel ugly.
    • Caesar cipher is implemented as a separate function, by which the change is passed This is your recommended mood Using a UO function, encapsulates uppercase and lowercase letters. A positive change is required.
    • Note that both I / O loop and cipher functions are quite low, which is good.

      Here's an example:

        #include & lt; stdlib.h & gt; # Include & lt; stdio.h & gt; # INFILE Define "in.txt" #define OUTFILE "out.txt" int cesar (int CR, int pie) {while ('a' and lieutenant; 0) change + = 26; if ('a' and lieutenant; = & Amp; amp; & amp;;; & lt; = 'z') return ('a '+ (CH -' A '+ Shift)% 26) Returns (' A '+ (ch -' A '+ Shift)% 26); Return ch;} int main () {if 'a' and lieutenant; = ch & amp; Amp; & amp;; CH & lt; = 'Z' FILE * fptr_in; FILE * fptr_out; Fptr_in = FOP N (INFILE, "R"); If (fptr_in == NULL) {fprintf (stderr, "% s can not open. \ N", INFILE); Exit (1); } Fptr_out = fopen (OUTFILE, "w"); If (fptr_in == NULL) {fprintf (stderr, "% s can not be created. \ N", OUTFILE); Exit (1); } (;;) {int ch = getc (fptr_in); If break (f == EOF); Puttak (Caesar (CH, 5), FTTR_out); } Fclose (fptr_in); fclose (fptr_out); Return 0; }    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -