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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:45:41+00:00 2026-05-17T19:45:41+00:00

mWhile attempting to make a backup copy of a file in C, I find

  • 0

mWhile attempting to make a backup copy of a file in C, I find that extra characters are sometimes generated by the algorithm below. I’ve also tried declaring the readBuffer within the while loop, but that did not solve the problem. Here’s an example of the issue.

Original File Contents

Hello there. 
My name is Alice. 
Done.

Backup File Contents

Hello there. 
My name is Alice.
Done.ice

As you can see, there are characters left in the last line from the previously buffered message. This only happens on the last lines of the file since any other time the buffer is filled with new contents. How can my logic below be corrected to resole this issue?

while(0 != bytesRead)
{      
    bytesRead = read(fdRead,readBuffer, BUFFER_SIZE);      
    if(0>bytesRead)
    {
      fprintf(stderr,"read() on '%s' for backup failed.\nError Info: %s\n",fileName,strerror(errno));
      exit(EXIT_FAILURE);
    }
    else if(0<bytesRead)
    {
      if(-1 == write(fdWrite,readBuffer,BUFFER_SIZE))
        {
          fprintf(stderr,"An error occurred while writing backup for '%s'.\nError Info: %s\n",fileName,strerror(errno));
          exit(EXIT_FAILURE);
        }
    }
}
  • 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-17T19:45:42+00:00Added an answer on May 17, 2026 at 7:45 pm

    Try: write(fdWrite,readBuffer, bytesRead).

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

Sidebar

Related Questions

I'm trying to implement the pseudo solution below in java as part of an
I am doing a parse program.this is my main file. I create an instance
I'm trying to develop a program that takes requests for data which is stored
can somebody explain why is the following trivial code (implementation of Euclid's algorithm to
I'm working up a game in XCode, and I want to add a helper
I have a constructor declaration as: MyConstuctor(int inDenominator, int inNumerator); and definition as MyConstuctor::MyConstuctor(int
In the implementation of binary search int search(int[] A, int K) { int l
I have a couple of questions actually. I have a class Dog with the
I'm having problems making a top level widget fade in, in TKinter. For some
I want to create a 3d scatter plot of spheres with their color being

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.