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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:23:32+00:00 2026-06-15T15:23:32+00:00

I wrote this code for overriding the cat command in Ubuntu. The following three

  • 0

I wrote this code for overriding the cat command in Ubuntu. The following three formats for cat instruction are working properly but rest are not working.

Working ones:

  • ./catf > File.txt
  • ./catf < File.txt

Not Working:

  • ./catf File1.txt > File.txt
  • ./catf File.txt

I am sending the terminal arguments to program as listed above. One other problem is every file also contains Enter your String: also which it shouldn’t.Here is code:

int main(int argc, char *argv[])
{
    int readbytes,fp;
    char buf[1024];
    if(argc==1)
    {
        printf("Enter your String :\n\n");
        readbytes=read(STDIN_FILENO,buf,1024);
        write(STDOUT_FILENO,buf,readbytes);
    }
    else if(argc==2)
    {
        fp=open(argv[1],O_RDONLY);
        dup2(0,fp);
        close(fp);
        readbytes=read(STDIN_FILENO,buf,1024);
        write(STDOUT_FILENO,buf,readbytes);
    }
    else if(argc==3)
    {
        if(argv[1][0]=='<')
        {
            fp=open(argv[2],O_WRONLY|O_CREAT,S_IRWXU);
            dup2(1,fp);
            close(fp);
            readbytes=read(STDIN_FILENO,buf,1024);
            write(STDOUT_FILENO,buf,readbytes);
        }
        else if(argv[1][0]=='>')
        {
            fp=open(argv[2],O_RDONLY);
            dup2(1,fp);
            close(fp);
            readbytes=read(STDIN_FILENO,buf,1024);
            write(STDOUT_FILENO,buf,readbytes);
        }
    }
    else if(argc==4)
    {
        printf("inside");
        fp=open(argv[1],O_RDONLY);
        dup2(0,fp);
        close(fp);
        fp=open(argv[3],O_WRONLY|O_CREAT,S_IRWXU);
        dup2(1,fp);
        close(fp);
        readbytes=read(STDIN_FILENO,buf,1024);
        printf("%c",buf);
        write(STDOUT_FILENO,buf,readbytes);
    }
    return 0;
}

UPDATE:

I made the changes suggested by William in the code and the rest of the code is working fine but this ./catf File.txt ">" File2.txt is still not working. Why is that?

if(argc==4)
    {
        printf("inside4");
        fp=open(argv[1],O_RDONLY);

        dup2(fp,0);

        close(fp);

        fp=open(argv[3],O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU);

        dup2(fp,1);

        close(fp);

        readbytes=read(STDIN_FILENO,buf,1024);

        //printf("%c",buf);

        write(STDOUT_FILENO,buf,readbytes);
    }

What is the reason for above mentioned problems?

  • 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-15T15:23:33+00:00Added an answer on June 15, 2026 at 3:23 pm

    When you invoke cat > file1.txt, *argv[1] is not <. Rather, argc is 1 and stdout is already associated with the file by the time main is called. If you want to pass < as an argument via the shell, you will need to quote it:

    $ cat '<' filename
    

    Also, your invocation of dup2 has the arguments reversed; STDOUT_FILENO should be the 2nd argument, not the first. The way you have it in your argc==2 clause is closing fp and reopening it to the original stdout.

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

Sidebar

Related Questions

I wrote this code for zoom in/out . it works but even with one
I wrote this code. The constructor works normally, but in the destructor I get
My brother wrote this code for me to show an example of polymorhism. But
Im new to ruby, wrote this code and works but i know is not
I wrote this code for zoom in / out and it suppesed to only
I wrote this code to create a custom annotation image - (MKAnnotationView *)mapView:(MKMapView *)mapView
I wrote this code in C# to encrypt a string with a key: private
I wrote this code that compiles on Solaris gcc it works fine too for
I wrote this code in my viewDidLoad function in my iOS project. lettersLeftLabel.text =
I wrote this code: ScheduledExecutorService ExtractorTimer=Executors.newScheduledThreadPool(1); final ScheduledFuture<?> SchedulerHandle; SchedulerHandle =ExtractorTimer.scheduleWithFixedDelay( new Runnable() {

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.