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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:11:12+00:00 2026-06-11T16:11:12+00:00

So this is one of the first programs I’ve ever used some self created

  • 0

So this is one of the first programs I’ve ever used some self created error checks however for some reason when I compile this and run it using:

./file test1.txt test2.txt 10

I get absolutely an error suggesting that the output file exists and I’ve checked the file and it doesn’t even when I change the name of the output file (second argument) I get nothing. Anyone who can help? I’ve been racking my brain for ages now. This is a UNIX homework assignment I’m compiling and running in Gentoo. I have it running in a VB and have a linked folder between my windows and linux OS’s.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define BUFFT 25

int main (int argc, char *argv[])
{
  int count;
  int readin;
  int writeout;

  printf ("This program was called  \"%s\".\n",argv[0]);

  if (argc > 1)
    {
      for (count = 1; count < argc; count++)
    {
      printf("argv[%d] = %s\n", count, argv[count]);
    }
    }
  else
    {
      perror("The command had no arguments.\n");
      exit(-1);

    }
    // check correct number of arguments parsed //


    if  (argc == 4)
    {
        printf("There are the correct number of arguments(4)\n");

        }
        else
        {
            perror("Not enough arguments! please try again \n");
            exit(-1);
            }
    //Check original file is there//

    int openFD = open(argv[1], O_RDWR);
    if (openFD <0)
    {
        perror("Error unable to read file \n");
        exit(-1);
    }

    //Check existence of output file, if it doesn't exist create it//

    int CheckFile = open(argv[2], O_RDONLY);
    if (CheckFile < 0)
    {
        perror("Error output file already exists \n");
        exit(-1);
        }
     else
     {
     int CheckFile = open(argv[2], O_CREAT);
     printf("The file has successfully been created \n");
     }

    //Create buffer

    int bufsize = atoi(argv[3]);
    char *calbuf;
    calbuf = calloc(bufsize, sizeof(char));

    //Read text from original file and print to output//

    readin = read(openFD, calbuf, BUFFT);
    if (readin < 0){
        perror("File read error");
        exit(-1);
    }
    writeout = write(openFD,bufsize,readin);
    if (writeout <0){
        perror("File write error");
        exit(-1);
    }


  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-11T16:11:13+00:00Added an answer on June 11, 2026 at 4:11 pm

    The open call for HANDLE CheckFile is printing Error File Exists. This is your problem. You are printing out the wrong statement when Output File Is Not Found and moreover you are exiting which prevents the code to create any.

    int CheckFile = open(argv[2], O_RDONLY);
    if (CheckFile < 0)
    {
        //Means the file doesn't exist
        int CheckFile = open(argv[2], O_CREAT);
        // Check for errors  here
    }
    

    And why are you trying to do this::

    writeout = write(openFD,bufsize,readin);
    

    when your HANDLE TO OUTPUT FILE IS CheckFile

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

Sidebar

Related Questions

So this is one of the first programs I've written in Python. I'm trying
This one is a huge issue: first off, while I know a little bit
I can't figure this one out. At first let me say that my cache
I have two arrays the first one has this structure Parameter Keys array array
Racking my brains on this one. I have the code below: the first stages
First of all I did gave a look at this one. But I didn't
I'm following a bioinformatics text, and this represents one of my first Perl scripts.
So this was one of my very first questions here, but I have a
This is my second question on Bamboo ( My First One ). My understanding
This is kinda....a two part question. The first one is much more important than

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.