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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:55:28+00:00 2026-06-17T21:55:28+00:00

Ok, I have a homework assignment to write a file in C using the

  • 0

Ok, I have a homework assignment to write a file in C using the POSIX API that asks for a filename to read from, a filename to write to and then copies one to the other. I have done this and it works great! I am trying to put some error checking in and I want to check to see if the file to be written to already exists and if it does, then ask if the user wishes to overwrite. The problem is that it always states that the file exists, even if it does not. The rest of the program works just fine. I have read a lot on here and found a lot of useful stuff on POSIX, but can’t find this type of problem to reference. Below is my code:

#include <fcntl.h>   // POSIX: give access to open
#include <unistd.h>  // POSIX: gives access to read, write, close
#include <stdio.h>   // POSIX: gives access to BUFSIZ

int main() {

int source = -1;
int target;
char sourcefile[50];
char targetfile[50];
char buff[BUFSIZ];
char ow[3];
size_t size;

printf("Please enter the name of the file you wish to read: ");
scanf( "%s", sourcefile );
printf( "\n" );
printf("Please enter the name of the file you wish to write to: ");
scanf( "%s", targetfile );
printf( "\n" );

source = open( sourcefile, O_RDONLY, 0);
//Test for existence of input file
if( source == -1 )
{
    perror( "Cannot find file" );
    return 1;
}
target = open( targetfile, O_WRONLY, 0644 );
//Test for existence of output file
if( target == 0 )
{
    perror( "File already exists" );
    printf( "Do you wish to overwrite? (yes or no): " );
    scanf( "%s", ow );

    if( strcmp( ow, "yes" ) == 0 )
    {
        target = open( targetfile, O_WRONLY | O_CREAT, 0644);   
    }else
    {
        printf( "Program Terminated!\n" );
        return 1;   
    }   
}else if( target == -1 )
{
    target = open( targetfile, O_WRONLY | O_CREAT, 0644);
}

while ((size = read(source, buff, BUFSIZ)) > 0) 
{
    write(target, buff, size);
}

close(source);
close(target);

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-06-17T21:55:29+00:00Added an answer on June 17, 2026 at 9:55 pm

    You can use open(2) with O_EXCL to create the purported new file. This will fail if the file already exists; in that case abort fatally. Otherwise you can write the desired file contents into it.

    If you want to be atomic, you can write the file contents into a temporary file (using tmpfile) and then atomically replace the created file with rename(2). That way the new file is either empty or a complete copy.


    An alternative (suggested by @R.) is to not bother opening the target file, copying into a temporary file, and then using link(2) instead of rename to attempt to put the new file in its destination location. This will fail if the destination already exists. This may have the (arguable) benefit that if someone else creates the target file in the meantime and doesn’t use the same care that we do, then you would be a bit more gentle in that case. (But if someone else wants stampedes around your filesystem without care, there’s only so much you can do.)

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

Sidebar

Related Questions

I have a homework assignment where I need to take input from a file
I have a homework assignment that I am supposed to write a http server
This is a homework assignment, I have to write a program that reads a
I have a homework assignment, and i am finished other then one question (see
Good day, Stack Overflow. I have a homework assignment that I'm working on this
A recent homework assignment I have received asks us to take expressions which could
Hello I have a homework assignment where I need to read two matrix .txt
I have to write this code for a homework assignment but I don't even
As part of a homework assignment, I have to write a C program in
This is part of a homework assignment. What we have to do is write

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.