Thread: program that XORs two given files and returns an output

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    program that XORs two given files and returns an output

    Hello. I study C.

    I am working on a program written in C that XORs two binary files until the length of the shorter one.

    Its inputs are names of two files entered by the user, say file1.ttt and file2.ttt, located in the same folder. Its output is a file called output.ttt, with the length of the shorter one from the first two. After XORing them it displays a message on a screen saying "File output.ttt created.".

    I have found some links:
    https://en.wikipedia.org/wiki/C_file_input/output
    C - File I/O
    c - Encoding and decoding the file with XOR in one program - Stack Overflow
    http://en.literateprograms.org/Simpl...cipher_%28C%29
    XOR File Encryption in C



    And here is my attempt:

    Code:
    #include <stdio.h>
    int main()
    {
       int n;
       char file_name1[25], file_name2[25];
       FILE *f1,*f2,*f3;
       printf("This program XORs two binary files until the length of the shorter one.\n\n");
       printf("Please enter the name of the first file:\n");
       gets(file_name1);
       printf("Please enter the name of the second file:\n");
       gets(file_name2);
    
       f1=fopen(file_name1,"r");
       f2=fopen(file_name2,"r");
       f3=fopen("output.ttt","w");
    
       n=sizeof(f1);
       if(sizeof(f1) >> sizeof(f2)){
                                      n=filesize(f2); /* now in n there is the fize of the shorter one */
                                    }
    
       
       /* now there should be something with the bits operation I have no clue about */
    
    
       fclose(f1);
       fclose(f2);
       fclose(f3);
    
       printf("File output.ttt created");
       return 0;
    }
    I know how to do it with Pascal, but in C people work with streams and that is difficult for me. Could you please help me with it? Thank you.
    Last edited by nerio; 09-29-2015 at 07:52 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by nerio View Post
    Code:
    n=sizeof(f1);
       if(sizeof(f1) >> sizeof(f2)){
    This is wrong. sizeof() is used to get the compile-time memory size of an object. It absolutely cannot be used to get the size of a file.

    To get the size of a file, you can use fseek() and ftell(). Unfortunately, using fseek with SEEK_END is not necessarily portable, according to the standard, but works on most common systems.

    You could also use stat(), but that function is posix, and not be fully portable on all systems.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    You don't even need to know how big the files are.

    Here's how I would do it:
    1. Read a character from each file.
    2. If you reached EOF on either file, you're done.
    3. Otherwise, XOR the characters together and write the character to the output.
    4. Repeat.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Quote Originally Posted by christop View Post
    You don't even need to know how big the files are.
    I admit that.


    Can someone please help me to finish the code? According to what I know it can be done by streams intead of XORing character by character..

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,745
    Here are two precursor exercises for you to do.

    1. Copy the file to output.ttt
    2. Copy the file to output.ttt, xor-ing each byte with a constant value of your choice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Well, this is 4 weeks old already without a solution.

    Why did you banned user CommonTater?
    Last edited by nerio; 10-27-2015 at 06:38 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by nerio
    Well, this is 4 weeks old already without a solution.
    Do you intend to finish this? Remember, the onus is on you, not anyone else in this forum community. Elkvis and christop have given you leads as to what you could try. Salem suggested breaking down the problem and experimenting (as per what is sometimes called Thomson's Rule for First-Time Telescope Makers: "It is faster to make a four-inch mirror then a six-inch mirror than to make a six-inch mirror.")
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,745
    Quote Originally Posted by nerio View Post
    Well, this is 4 weeks old already without a solution
    We're your tour guides, not your mules.

    If it's 4 weeks old without a solution, then the obvious question is what have you been doing for 4 weeks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Quote Originally Posted by Salem View Post
    If it's 4 weeks old without a solution, then the obvious question is what have you been doing for 4 weeks.
    I have also other things to do. I am a beginner in C and getting one line advices online while studying alone is time consuming.

    I don't intend anyone to finish it, I just have no clue how to continue.

    I already know that XOR between two bytes can be done by that operator: ^= .

    Copying files I tried here: copying binary file in C, copying binary file in C.

    According to what I know there exists something called buffer in C so that I do not have to XOR each character one by one.
    Last edited by nerio; 10-28-2015 at 02:15 AM.

  10. #10
    Registered User
    Join Date
    Oct 2015
    Posts
    28
    Your clue is in post #3, it's really that simple. The sooner you accept that, the sooner you can start learning C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mailing Label Program - Input & Output Files
    By beatbox32 in forum C Programming
    Replies: 5
    Last Post: 07-14-2011, 07:54 PM
  2. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  3. Replies: 5
    Last Post: 08-14-2009, 03:15 AM
  4. S_ISDIR returns directories and files?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2009, 01:56 PM
  5. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 05:18 AM