How to extend this code for doing "cat"

Discussion in 'C Programming' started by Elephant, Jan 3, 2006.

  1. Elephant

    Elephant Guest

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

    int main(int argc, char *argv[]) {
    int fd1, n;
    char sl;

    if (argc < 2) {
    printf("Koristenje: vjezba4 <ime_datoteke>\n");
    exit(0);
    }

    fd1=open(argv[1], O_RDONLY);
    if (fd1 < 0) {
    perror("open");
    exit(-1);
    }

    while((n=read(fd1, &sl, 1)) > 0) {
    write(STDOUT_FILENO, &sl, 1);
    }

    if (n == -1) {
    perror("read");
    }

    close(fd1);
    exit(0);
    }How to extend this code for doing "cat"program starts by entering a file
    name or more of them unless it s started with one filename it prints the
    source of the file. If there is no filename in program the stdin will copy
    the stdout
    Elephant, Jan 3, 2006
    #1
    1. Advertising

  2. Elephant

    Guest

    Elephant <> wrote:

    > How to extend this code for doing "cat"program starts by entering a file
    > name or more of them unless it s started with one filename it prints the
    > source of the file.


    Check the value of argc; if no extra arguments are specified (argc==1), read
    from the 'stdin' FILE. If argc is bigger then one, read file names one by
    one from argv, open the files and handle them as you do now.

    > If there is no filename in program the stdin will copy the stdout


    You probably mean 'stdin' instead of 'stdout' here

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    , Jan 3, 2006
    #2
    1. Advertising

  3. Elephant

    clayne Guest

    Elephant wrote:
    > if (argc < 2) {
    > printf("Koristenje: vjezba4 <ime_datoteke>\n");
    > exit(0);
    > }
    > How to extend this code for doing "cat"program starts by entering a file
    > name or more of them unless it s started with one filename it prints the
    > source of the file. If there is no filename in program the stdin will copy
    > the stdout


    I typically just utilize the following (using stdio for this example,
    but it shouldn't matter):

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char **argv)
    {
    FILE *f_in;
    char buf[256];

    if (argc > 1 && *argv[1] != '-') {
    if ((f_in = fopen(argv[1], "r")) == NULL)
    exit(EXIT_FAILURE); /* or other handling */
    } else
    f_in = stdin;

    while (fgets(buf, sizeof(buf), f_in) != NULL)
    fprintf(stdout, "%s", buf);

    exit(EXIT_SUCCESS); /* or simply return */
    }


    build02-sol8-x86:/tmp $ ./c
    duh
    duh
    ^D
    build02-sol8-x86:/tmp $ ./c -
    test
    test
    ^D
    build02-sol8-x86:/tmp $ echo test | ./c
    test
    build02-sol8-x86:/tmp $ echo test | ./c -
    test
    build02-sol8-x86:/tmp $ echo test_test >tmp
    build02-sol8-x86:/tmp $ ./c tmp
    test_test
    build02-sol8-x86:/tmp $ cat tmp | ./c
    test_test
    build02-sol8-x86:/tmp $ ./c | ./c
    1
    2
    ^D
    1
    2
    build02-sol8-x86:/tmp $
    clayne, Jan 4, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yadagiri Rao KP
    Replies:
    3
    Views:
    401
    Joseph Millar
    Aug 9, 2003
  2. Spendius
    Replies:
    2
    Views:
    2,958
    Rogan Dawes
    Dec 13, 2004
  3. Replies:
    1
    Views:
    362
    Sudsy
    Dec 19, 2004
  4. Saqib Ali
    Replies:
    9
    Views:
    494
    Michael Torrie
    Jan 16, 2012
  5. naruto canada

    Why does this code works without cat ?

    naruto canada, Apr 14, 2012, in forum: Perl Misc
    Replies:
    46
    Views:
    1,078
    Eric Pozharski
    May 2, 2012
Loading...

Share This Page