Thread: Encoding text also file write read problem

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    95

    Encoding text also file write read problem

    I want to encrypt text that is read from in.txt then write to out.txt
    Encryption type is every ascii plus five if char btw u and z , u=a,v=b,......,z=f where is my problem?
    Besides, can we do using modulos operator(%) instead of char-20 if char btw u and z , u=a,v=b,......,z=f where is my problem?
    my code is below thank you all appreciated answers.
    text is in.txt that test abcde
    Code:
    #include <stdio.h>
    
    
    
    
    int main(){
    
    
        char ch;
    
    
        FILE *fptr_in;
        FILE *fptr_out;
    
    
        if((fptr_in=fopen("in.txt","r"))==NULL)
            printf("File cannot be opened\n");
        else if((fptr_out=fopen("out.txt","w"))==NULL)
            printf("File cannot be opened\n");
        else
            fscanf(fptr_in,"%c",&ch);
        while(!feof(fptr_in)){
    
    
            if('a'<=ch && ch<='z'){
                fscanf(fptr_in,"%c",&ch);
            if(ch==' ')
                fprintf(fptr_out, "%c", ' '));
            else if('u'<=ch && ch<='z')
                fprintf(fptr_out, "%c", (char)((int)ch-20));
            else
            fprintf(fptr_out, "%c", (char)((int)ch+5));
    }
        }
    
    
        fclose(fptr_in);
            fclose(fptr_out);
    
    
        return 0;
    }
    Last edited by Ph0x; 10-08-2014 at 11:15 AM.

  2. #2
    Registered User
    Join Date
    Mar 2014
    Posts
    95
    new code
    Code:
    #include <stdio.h>
    
    
    int main(){
    
            char ch;
            int v;
    
            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 cannot be opened\n");
            else
                fscanf(fptr_in,"%c",&ch);
            while((v=fgetc(fptr_in))!=EOF){
                    if(ferror(fptr_in)){
                        printf("Error");
                        return -1;
                    }
                    else{
    
                if('a'<=ch && ch<='z'){
                    fscanf(fptr_in,"%c",&ch);
                if(ch==' ')
                    fprintf(fptr_out, "%c", ' ');
                else if('u'<=ch && ch<='z')
                    fprintf(fptr_out, "%c", (char)((int)ch-20));
                else
                fprintf(fptr_out, "%c", (char)((int)ch+5));
                }
        }
            }
    
            fclose(fptr_in);
                fclose(fptr_out);
    
            return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,035
    v and ch are two different variables!
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class FILE to write in DOS encoding.
    By ArnoldRich in forum C Programming
    Replies: 5
    Last Post: 12-13-2013, 10:30 AM
  2. Replies: 2
    Last Post: 11-29-2012, 11:45 PM
  3. Replies: 3
    Last Post: 10-31-2011, 04:33 AM
  4. read write to file problem
    By xniinja in forum C Programming
    Replies: 1
    Last Post: 06-09-2010, 11:01 AM
  5. what should i do when .....(read/write txt file problem)
    By Jasonymk in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2003, 08:55 PM