Thread: Having problems manipulating two files - i'm a beginner.

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    Unhappy Having problems manipulating two files - i'm a beginner.

    Hi
    I am trying to open two files in binary read mode and then XOR the contents of these files, character by chharacter, and store the results in a third file (by opening it in Binary write mode). Although i can open the two files but get a whole lot of garbage in the resultant file which is supposed to be having the XORed results. please help me.


    Thanks in advance
    Manu1

    code:-

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    
    void main(void)
    {
      FILE *fptr, *fptr1, *fptr2, *fptr3, *fptr4;
      char plain[5000], ke[5000], cipher[5000];
      int i = 0, j=0, k=0, pt1[500], kt1[500];
      long int ch, ch1, ch2;
      clrscr();
      printf("\n ENTER PLAIN TEXT (PRESS ENTER TWICE TO EXIT):\n");
      fptr = fopen("ptxt.txt", "w");
      fflush(fptr);
      do {
          fputs(plain, fptr);
          fputs("\n", fptr);
          }
      while(strlen( gets(plain) ) > 0);
      fclose(fptr);
     // fflush(fptr);
      printf("\n ENTER KEY (PRESS ENTER TWICE TO EXIT):\n");
      fptr1 = fopen("ktxt.txt", "w");
      fflush(fptr1);
      do {
           fputs(ke, fptr1);
           fputs("\n", fptr1);
         }
      while(strlen( gets(ke) ) > 0);
      fclose(fptr1);
      //fflush(fptr1);
    
      fptr4 = fopen("ctxt.txt", "wb");
      fptr2 = fopen("ptxt.txt", "rb");
      fptr3 = fopen("ktxt.txt", "rb");
    
      fflush(fptr4);
    
      while (ch = getc(fptr2) != EOF)
      {
        ch1 = getc(fptr3) != EOF;
        cipher[i] = ch ^ ch1;
        i++;
        fwrite(cipher, sizeof(cipher), 1, fptr4);
       // i = j;
      }
    /*  while (ch1 = getc(fptr3) != EOF )
      {
        kt1[k] = ch1;
        k++;
      }
        cipher[i] = ch ^ ch1;
       // i++;
       // putc(cipher, fptr4);
        i++;
      }*/
     /* for(k=0; k<j; k++)
      {
        putc(cipher[k], fptr4);
      }*/
      fclose(fptr2);
      fclose(fptr3);
      fclose(fptr4);
     // fflush(fptr4);
    
    }
    Last edited by manu1; 08-12-2003 at 06:02 AM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Can you post your code?

    Anoop.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    How to get the XORed output in a file.

    Hi Salem

    Thanks a lot for your help.

    I can create both files perfectly now, but im still getting the same type of abnormal data when i access the third file (which is supposed to have XORed data from both the files instead. Pl. help me with this. Following is my amended code:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    
    int main(void)
    {
      FILE *fptr, *fptr1, *fptr2, *fptr3, *fptr4;
      char plain[5000], ke[5000], cipher[5000];
      int i = 0, j=0, k=0, pt1[500], kt1[500];
      long int ch, ch1, ch2;
      clrscr();
      printf("\n ENTER PLAIN TEXT (PRESS ENTER TWICE TO EXIT):\n");
      fptr = fopen("ptxt.txt", "w");
      
      while(fgets(plain, sizeof(plain),stdin)!= NULL)
      {
          if(plain[0]=='\n')
          break;
          fputs(plain, fptr);
          fputs("\n", fptr);
          }
     
      fclose(fptr);
     
      printf("\n ENTER KEY (PRESS ENTER TWICE TO EXIT):\n");
      fptr1 = fopen("ktxt.txt", "w");
      
      while(fgets(ke, sizeof(ke),stdin)!= NULL)
      {
          if(ke[0]=='\n')
          break;
          fputs(ke, fptr1);
           fputs("\n", fptr1);
      }
     
      fclose(fptr1);
      
    
      fptr4 = fopen("ctxt.txt", "wb");
      fptr2 = fopen("ptxt.txt", "rb");
      fptr3 = fopen("ktxt.txt", "rb");
    
      
    
      while(ch = fgets(plain, sizeof(plain),stdin)!= NULL)
      {
        
        if(ch1 = fgets(ke, sizeof(ke),stdin)!= NULL)
       { cipher[i] = ch ^ ch1;}
        i++;
        fwrite(cipher, sizeof(cipher), 1, fptr4);
      
      }
    
      fclose(fptr2);
      fclose(fptr3);
      fclose(fptr4);
     
    }
    I shall be very grateful for this help.
    Regards

    Manu

    I have tried to improve the listing of the program so as to make it more simple to read. Your help will be highly appreciated.
    Thanks & regards
    Manu
    Last edited by manu1; 08-12-2003 at 09:58 AM.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    your code is very difficult to read, please edit it and surround in the appropriate tags.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Having problems manipulating two files - i'm a beginner.

    Hey manu1, didn't you like the help that stober gave you at ProgramersHeaven with the question you posted
    August 12, 2003 at 3:46:23 AM

    It looked like by 8AM you had code working.

    Now you post your original code here?

    The original post, for those who are interested:
    http://www.programmersheaven.com/c/M...ing=A0003F0001
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    And now you start a new thread?!?!?!!?

    Keep using the same thread!
    http://cboard.cprogramming.com/showt...threadid=43344
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    Thanks

    Thanks a lot mates.
    Much appreciated.

    Manu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  3. beginner problems with array elements...
    By grnphrog in forum C Programming
    Replies: 4
    Last Post: 01-27-2003, 02:38 PM
  4. Manipulating Files
    By JoshuaNSW in forum C Programming
    Replies: 2
    Last Post: 01-24-2002, 02:01 AM
  5. Problems with resource files
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 08-31-2001, 08:45 AM