Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7658141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:09:46+00:00 2026-05-31T13:09:46+00:00

For a lab I’ve got to do, I need to create a program that

  • 0

For a lab I’ve got to do, I need to create a program that will take a simple string from a text file and encrypt it using a key – a number between 0 and 255. It will read the file into an array and encrypt (or decrypt) this array into another array by XOR-ing each byte with the key. In the end, it writes the modified array into a second file.

I’ve mostly got it – what I have below compiles just fine. It doesn’t, however, copy anything to the second file. Help!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CRYPT(a, b) (a ^ b)

int main(int argc, char *argv[])
{
    FILE *fp1, *fp2;
    int a[100], b, key;
    int i = 0;

    // opens file containing string to be encrypted
    if((fp1 = fopen(argv[2], "rb")) == NULL)
    {
            printf("Error - could not open file or file does not exist\n");
            return;
    }

    // opens file encrypted string will be saved to
    fp2 = fopen(argv[3], "wb");

    // converts string to integer
    key = atoi(argv[1]);

    while(fread(a, sizeof(a), 100, fp1))
    {
            while (i != '\0');
            {
                    b = CRYPT(a[i], key);
                    fwrite(&b, sizeof(a), 1, fp2);
                    i++;
            }
    }


    return 0;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:09:47+00:00Added an answer on May 31, 2026 at 1:09 pm

    I think the problem lies here –

    while (i != '\0');
    

    You are initializing i to 0 and in the while loop you are checking whether or not i is equal to NULL. The integer value of NULL, or \0 is 0. As a result, the expression is false and your loop is never being executed.

    Also remove the extra semi-colon at the end of this while loop.

    From the reference –

    size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

    Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr.
    The postion indicator of the stream is advanced by the total amount of bytes read.
    The total amount of bytes read if successful is (size * count).

    So you also need to change your fread function to this –

    fread(a, sizeof(int), 100, fp1)
    

    Similarly, you also need to change your fwrite –

    fwrite(&b, sizeof(int), 1, fp2);
    

    The edited code should look something like this –

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define CRYPT(a, b) (a ^ b)
    
    int main(int argc, char *argv[])
    {
        FILE *fp1, *fp2;
        int a[100], b, key;
        int i = 0;
        int data_read = 0;
    
        // opens file containing string to be encrypted
        if((fp1 = fopen(argv[2], "rb")) == NULL)
        {
                printf("Error - could not open file or file does not exist\n");
                return;
        }
    
        // opens file encrypted string will be saved to
        fp2 = fopen(argv[3], "wb");
    
        // converts string to integer
        key = atoi(argv[1]);
    
        while( (data_read = fread(a, sizeof(int), 100, fp1)) > 0 )
        {
                while(i < data_read)
                {
                        b = CRYPT(a[i], key);
                        fwrite(&b, sizeof(int), 1, fp2);
                        i++;
                }
    
                i=0;
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Occasionally in our lab, our postgres 8.3 database will get orphaned from the pid
I have a problem for a university lab; Write a short program that outputs
I am Working On a lab. A father process will create two son processes
I want to create a lab write-up with LaTeX in Ubuntu, however my text
git diff master..lab It will produce the diff between the tips of the two
I am using a preview script via http://cssglobe.com/lab/tooltip/03/ The problem is that when I
I am receiving lab HL7 messages from a static host and a dynamic port.
I work for a medical lab company. They need to be able to search
This is a lab assignment I am stuck on. I need to accept this
I have a lab values table that I would like to query and get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.