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 6685225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:02:01+00:00 2026-05-26T05:02:01+00:00

I have an assignment that has asked me to copy a file using buffered

  • 0

I have an assignment that has asked me to copy a file using buffered i/o. It has multiple requirements:

  1. Take one parameter and an optional second
  2. Open the first parameter for reading
  3. Open the second for writing
  4. If there is no second parameter make a new file called prog1.out
  5. Use a buffer size of 20 bytes
  6. When copying the file, print any buffer starting with the characters “rwxr”
  7. close all opened files before exiting.

The problem I’m having is with number six, I’ve looked around and can’t figure this out. I’ve tried memchr but I don’t think I’m on the right track. If anyone can help push me in the right direction I’d be grateful.

This is my code:

# include <stdlib.h>
# include <stdio.h>

int main(int argc, char *argv[])
{

    FILE *readfile, *writefile;
    char buffer[1024];
    int fileSize;
    int readResult;
    int writeResult;

    // making sure arguments exist
    if (argc < 2|| argc > 3){
            printf("This program takes either 1 or 2 arguments.\n");
            exit(1);
    }

    //Opening file for reading
    readfile = fopen(argv[1], "r");
    if (!readfile) {
            printf("Unable to open file %s.\n", argv[1]);
            exit(1);
    }

    //finding the file size
    fseek (readfile, 0, SEEK_END);
    fileSize = ftell (readfile);
    fseek (readfile, 0, SEEK_SET);

    // read the file
    readResult = fread(buffer, 20, fileSize/20, readfile);
    if (readResult == 0) {
            printf("A read error occured.\n");
            exit(1);
    }

    //check to see if there is a second parameter (argument)
    if (argc == 3) {
            writefile = fopen(argv[2], "w");

            if (!writefile) {
                    printf("Unable to open file  %s.\n", argv[2]);
                    exit(1);
            }

            writeResult = fwrite(buffer, 20, fileSize/20, writefile);
            if (writeResult != readResult) {
                    printf("A write error occured.\n");
                    exit(1);
            }
            printf("File %s successfully copied to %s.\n", argv[1], argv[2]);
    }
    else {
            writefile = fopen("program1.out", "w");

            if (!writefile) {
                    printf("Unable to open file program1.out\n");
                    exit(1);
            }

            writeResult = fwrite(buffer, 20, fileSize/20, writefile);
            if (writeResult != readResult) {
                    printf("A write error occured.\n");
                    exit(1);
            }
            printf("File %s successfully copied to %s.\n", argv[1], "program1.out
    }
    fclose(readfile);
    fclose(writefile);
    exit(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-26T05:02:02+00:00Added an answer on May 26, 2026 at 5:02 am

    There’s the naive way:

    if(buffer[0] == 'r' && buffer[1] == 'w' 
       && buffer[2] == 'x' && buffer[3] == 'r') {
       //do something
    }
    

    But take a look at strncmp(), which you can use for comparing parts of a string.

    • remember to first check if you have read at least 4 chars into the buffer. e.g. if the file is 21 bytes long, your 2. fread might only read 1 character, and you shouldn’t compare against the other 3 chars in the buffer.

    • If you print out the buffer with e.g. printf or puts or any other function that expects a string, the buffer needs to end with a ‘\0’ byte, otherwise the string functions doesn’t know when to stop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Java assignment that has to be done using AWT. I
Using C#, say you have a ComboBox that has a DropDownStyle set to DropDown
Im almost done with a homework assignment that multiplies polynomials and has to have
I have a class that has no default constructor or assignment operator so it
I have an assignment that my teacher has told us to use this in
I currently have a school assignment that involves both PHP and asp.net. Now the
I'm working on an assignment that is telling me to assume that I have
For an assignment in college, we have to make a script in Perl that
I have an assignment in a language-independent class, and part of it is using
I have an assignment to create a GUI using MATLAB GUIDE and am having

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.