Greetings!
I need to write some C code that will decide between either reading from
stdin or take a file name from argv and process it.
The program needs to work like all of the typical unix utilities where
the file to process is either provided from a pipe/stream or argv.
Utilities like lp, tr, grep, etc.
So, when the command:
cat filename | grep -i > filename2
or
grep -i filename > filename2
executes, this acts like the programmtic interface i need.
I have tried everything I can think of from getc, getchar, read, gets,
etc. which all did not work. I test for EOF or NULL on the return values
and the program just hangs on a terminal read. So, the command:
cat filename | testprogram -dvalue
hangs on the reading from stdin and I never get to test for EOF.
What am i missing?
Do I need to set some type of fcntl flag and control the stdin flow
similar to a wait/nowait process?
BTW, i am compiling this on HP-UX B.11.00 Unix.
I need an answer ASAP!
Thanks in advance! 11 4326
David Warner wrote: Greetings!
I need to write some C code that will decide between either reading from stdin or take a file name from argv and process it.
The program needs to work like all of the typical unix utilities where the file to process is either provided from a pipe/stream or argv.
Utilities like lp, tr, grep, etc.
So, when the command:
cat filename | grep -i > filename2
or
grep -i filename > filename2
executes, this acts like the programmtic interface i need.
I have tried everything I can think of from getc, getchar, read, gets, etc. which all did not work. I test for EOF or NULL on the return values and the program just hangs on a terminal read. So, the command:
cat filename | testprogram -dvalue
hangs on the reading from stdin and I never get to test for EOF.
What am i missing?
Probably you can show your code to see what's missing. This one works.
$ cat simplecat.c
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 100
int main(int argc, char ** argv)
{ char buf[BUF_SIZE + 1];
FILE * inp;
if(argc == 2)
{ inp = fopen(argv[1], "r");
if(!inp)
{ return EXIT_FAILURE;
}
}
else
{ inp = stdin;
}
while(fgets(buf , sizeof buf, inp))
{ printf("%s", buf);
}
fclose(inp);
return EXIT_SUCCESS;
}
ishs
In article <da************ *************** *@corp.supernew s.com>,
David Warner <da******@westb ase.com> wrote: I need to write some C code that will decide between either reading from stdin or take a file name from argv and process it.
I have tried everything I can think of from getc, getchar, read, gets, etc. which all did not work. I test for EOF or NULL on the return values and the program just hangs on a terminal read.
The usual thing to do it to see whether there's a filename argument
and read from stdin if there isn't. Trying to read is useless, since
there will always be either a file or a terminal on stdin (for typical
operating systems).
-- Richard
I did the exact same thing on HP-UX... this is how my code starts...
main(int argc, char *argv[])
{
if (argc > 1) {
if the arguments are greater than 1 (1 being the command) then process
them, else use a default. I wrote this to check the similarties
between 2 files. They are allways the same, but this way I can use it
to check whatever 2 files I wish. I can option to add file names or
use defaults.
Hope this helps,
-#2pencil- http://www.akroncdnr.com
In article <d9***********@ pc-news.cogsci.ed. ac.uk>, ri*****@cogsci. ed.ac.uk (Richard Tobin) writes: The usual thing to do it to see whether there's a filename argument and read from stdin if there isn't. Trying to read is useless, since there will always be either a file or a terminal on stdin (for typical operating systems).
Unix is atypical? A great many Unix processes are executed with
neither a file nor a terminal associated with stdin. Some have
nothing associated with stdin (for Unix implementations , this
means descriptor 0 does not refer to an open entry in the file
table); many more have other types of system objects associated
with stdin.
The language requires that (for a hosted implementation) at program
startup stdin be "predefined " and "need not be opened explicitly".
That doesn't mean that it must refer to something that can success-
fully be read. It's entirely possible that the first attempt to
read from stdin will result in EOF or an error.
None of that helps with the OP's question, of course.
--
Michael Wojcik mi************@ microfocus.com
This book uses the modern technology to explain the phenomemon in the world
of Japanese animation. If you love anime so much, you'd better read this.
After you read it, you may agree that is destroying the dream of the child.
Needs Chinese viewing system. -- The Goodboy Scientific
In article <da*********@ne ws4.newsguy.com >,
Michael Wojcik <mw*****@newsgu y.com> wrote: Trying to read is useless, since there will always be either a file or a terminal on stdin (for typical operating systems).
Unix is atypical? A great many Unix processes are executed with neither a file nor a terminal associated with stdin.
I was referring to programs run from the command line, which was what
the OP seemed interested in.
-- Richard
In article <da**********@p c-news.cogsci.ed. ac.uk>, ri*****@cogsci. ed.ac.uk (Richard Tobin) writes: In article <da*********@ne ws4.newsguy.com >, Michael Wojcik <mw*****@newsgu y.com> wrote: Trying to read is useless, since there will always be either a file or a terminal on stdin (for typical operating systems).
Unix is atypical? A great many Unix processes are executed with neither a file nor a terminal associated with stdin.
I was referring to programs run from the command line, which was what the OP seemed interested in.
Perhaps. We all write code that makes some assumptions. I merely
wanted to point out that this one - that stdin could be read
successfully - was not always justified.
It's not true of programs started from the Unix command line, either.
In ksh:
$ cat <&-
cat: cannot stat
Does anyone do that? Yes. (After all, the operator is there for a
reason.) I recently had to use this myself, to run a program (that
I couldn't modify) in the background with no controlling terminal
association.
Many Unix programs can assume that stdin is associated with a
readable entity at program startup, because their failure mode if
it is not is acceptable. Some cannot. I've dealt with programs
that had security holes if they opened a file and it received
descriptor 0, 1, or 2, because they assumed that stdin, stdout,
and stderr were valid at program startup.
--
Michael Wojcik mi************@ microfocus.com
Thanks for your prompt reply and thanks for your invitatin to your
paradise. Based on Buddihism transmigration, I realize you, European,
might be a philanthropist in previous life! -- supplied by Stacy Vickers
In article <da********@new s4.newsguy.com> ,
Michael Wojcik <mw*****@newsgu y.com> wrote: Perhaps. We all write code that makes some assumptions. I merely wanted to point out that this one - that stdin could be read successfully - was not always justified.
The assumption relevant to this discussion is not that stdin can be
read. On the contrary, it's that the fact that stdin *can* be read
implies nothing about whether it's the intended input for the program.
I see no harm in ignoring the corner cases of unix when explaining how
to decide where to read input from.
To reiterate the possibly lost point: decide where to read from based
on the arguments, not on the state of stdin.
-- Richard
Richard Tobin wrote: Michael Wojcik <mw*****@newsgu y.com> wrote:
Perhaps. We all write code that makes some assumptions. I merely wanted to point out that this one - that stdin could be read successfully - was not always justified.
The assumption relevant to this discussion is not that stdin can be read. On the contrary, it's that the fact that stdin *can* be read implies nothing about whether it's the intended input for the program. I see no harm in ignoring the corner cases of unix when explaining how to decide where to read input from.
To reiterate the possibly lost point: decide where to read from based on the arguments, not on the state of stdin.
On most systems it is possible to decide whether or not stdin is
connected to an interactive keyboard with slightly non-portable
code (which in turn can be replaced by something that reports
not-a-keyboard portably). This allows some easy up-front
decisions, such as whether to emit a help screen and exit. Very
useful for filters.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
CBFalconer wrote: Richard Tobin wrote:
Michael Wojcik <mw*****@newsgu y.com> wrote:
Perhaps. We all write code that makes some assumptions. I merely wanted to point out that this one - that stdin could be read successfully - was not always justified.
The assumption relevant to this discussion is not that stdin can be read. On the contrary, it's that the fact that stdin *can* be read implies nothing about whether it's the intended input for the program. I see no harm in ignoring the corner cases of unix when explaining how to decide where to read input from.
To reiterate the possibly lost point: decide where to read from based on the arguments, not on the state of stdin.
On most systems it is possible to decide whether or not stdin is connected to an interactive keyboard with slightly non-portable code (which in turn can be replaced by something that reports not-a-keyboard portably). This allows some easy up-front decisions, such as whether to emit a help screen and exit. Very useful for filters.
Pray tell. Given foo, a program which takes no arguments and expects
input from stdin, I might invoke it two ways..
foo
in which case I am expected to enter data from the keyboard or..
foo < infile
in which case data comes to stdin from a file. How can I know from
within foo, which way foo was invoked?
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: lamar_air |
last post by:
I have a fortran executable which when run from cmd it asks for a
series of parameters which you enter then hit enter.
From my python cgi script i want to be able to run the executable.
Enter the 4 or 5 parameters needed then end the executable and
redirect to another web page which will display some results given
from an output file from the...
|
by: J.R. |
last post by:
Hi folks,
The python can only support passing value in function call (right?), I'm
wondering how to effectively pass a large parameter, such as a large list or
dictionary?
It could achieved by pointer in C++, is there such way in Python?
Thansk in advance.
J.R.
|
by: lonelyplanet999 |
last post by:
Hi,
I'm a newbie of perl language and I would like to ask the differences
in below pieces of code:
1.
read(STDIN, $stuff, $ENV{'CONTENT_LENGTH'});
@pairs = split(/\&/, $stuff);
2.
|
by: Panos Laganakos |
last post by:
Hello,
I'd like to know how its possible to pass a data attribute as a method
parameter.
Something in the form of:
class MyClass:
def __init__(self):
self.a = 10
|
by: Cameron Laird |
last post by:
Question:
import subprocess, StringIO
input = StringIO.StringIO("abcdefgh\nabc\n")
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(, stdout = subprocess.PIPE,
stdin = response)
| |
by: Matthew Wilson |
last post by:
Lately, I've been writing functions like this:
def f(a, b):
assert a in
assert b in
The point is that I'm checking the type and the values of the
parameters.
|
by: arne |
last post by:
Hi all,
imagine I call a function, but omit one of the parameters, like:
foo.c:
void foo( int a, int b ) {
/* do something with a and b */
return;
}
|
by: Peter Blues |
last post by:
I'm trying to write from keyboard input to a txt file. I don't understand
why the while loop below doesn't achieve this.
I..
1: open the file for writing through FILE pointer outstream
2: I get chars one at a time as ints(ascii) through fd using getchar()
3: Copy from fd to FILE *outstream using fputc()
4: Finally close outstream.
I feel...
|
by: bharathv6 |
last post by:
i need to do is modify the image in memory like resizing the image in memory etc ... with out saving it disk as i have to return back the image with out saving it disk
PIL supports the use of StringIO objects being passed in place of file
objects. StringIO objects are binary strings of variable length that are kept
in memory so i saved the...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |