473,411 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How to extend this code for doing "cat"


#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
Jan 3 '06 #1
2 1511
Elephant <Fi*****@hotmail.com> 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 '06 #2
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 $

Jan 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jesse Noller | last post by:
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = list2 = list3 = main_list =
1
by: Darren Lew | last post by:
Hi there, I have this problem which is really driving me nuts for a couple of days. Glad if any help or suggestion is given Javascript will give me a pop-up error "Access is denied" when i try...
18
by: Clark Nu | last post by:
It seems that when I define a fuction,I can set a default value to some of the peremeters.When I call the fuction without some of them,the fuction will use the default value automaticlly then...
2
by: Martin Kulas | last post by:
Hallo! I have a problem with Py_BuildValue: I want to convert an unsigned int to a PyObject *. http://docs.python.org/api/arg-parsing.html says that I can use "I" as a format string. But it...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression...
0
by: Gawn | last post by:
Dear all, Greeding from Thailand. Need help for my news script. I am trying to display news which keywords match the current page keywords. I am using Dreamweaver 8 and PhpMyAdmin to manage MySQL....
1
by: Laurence | last post by:
Hi folks, I did "cat /etc/hosts /dev/kmem" on AIX v5.3 TL5 SP4 for simulating the system crash. But, it didn't work. The error message was shown below: cat: 0652-054 Cannot write to output....
5
by: planetbrett | last post by:
I have read through php.net manuals and have not see any mention about what these operands actually do. I have seen them used in a bunch of different code lately and don't really understand. ...
3
by: Willy | last post by:
I have a website that uses querystrings on a certain page to show multiple contents. I have created mapped pages to hide the gory details of these querystrings. So instead of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.