Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Write a program that works like cat

    Hello everyone,
    I would like to ask you about program which should work as cat command in linux. I know that first of all i have to use
    and also for
    and put this to the table and after that do "compile" function with those arguemnts, am I right ? but i have a problem with that:

    #include <stdio.h>
    #include <fcntl.h>
    int main( int argc, const char* argv[] ) 
        const char* tab1[512]=execl("/bin/cat","cat",argv[1],(char*)NULL);
        const char* tab2[512]=execl("/bin/cat","cat",argv[2],(char*)NULL);
        return 1;
    #define BLOCK 512
    char *fd1,*fd2;
        char buffer[BLOCK];
        int i=0;while(i<511){buffer[i]='0';i++}
        int n;
            while ((n = read(fd1, buffer, BLOCK)) > 0)
                write(fd2, buffer, n);
    Last edited by just_jones; 12-08-2012 at 03:17 PM. Reason: change mistake in my code (increment i)

  2. #2
    Linux Enthusiast
    Join Date
    Dec 2011
    Turtle Island West
    I dunno?

    What is your problem with that? Does it not compile properly?

    More info please. What exactly are you trying to accomplish? That copyfile() looks broken to me, as do the calls to execl(). I've been programming for 30 years and never used execl() in my recollection.

    If you're just trying to dump fd1 and fd2 to stdout, try

    fprintf ( stdout, "%s%s", fd1, fd2 );

    but, unfortunately, I don't think execl() works the way you think it does. Try the manpages: "man execl".

    Check out memset(buffer, 0, BLOCK) also. It's way prettier than
    int i=0;while(i<511){buffer[i]='0';i++} . Ugh.

    It looks like you're trying to read 2 files in and then dump them to the screen. If so, you're way off base. Try "man 2 open", "man 2 read", and "man 2 close". And if you want to see how cat really works, download the GNU coreutils and have a close look.

    Good luck, n00b, and don't give up. Look at the source code of stuff that does similar things to what you're trying to accomplish. There's stuff that's mostly broken, stuff that works, and there's stuff that _REALLY_ works. There are some very clever people out there. Avail yourself of them, their poetry (AKA code) speaks for itself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts