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 $