Computing.Net > Forums > Programming > Linux: Sending a file as STDIN

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Linux: Sending a file as STDIN

Reply to Message Icon

Name: SN
Date: November 21, 2003 at 11:02:08 Pacific
OS: who
CPU/Ram: knows
Comment:

Hiya fellas-
So I have this project I need to do, where the teacher sends the input a weird way and I wondered if anybody knows anything about it:

The input is supposed to be read using STDIN (I'm writing it in perl but I could do it in java as well), but he is doing it some way in linux where you can specify a file on the command line when you call the program and linux sends the file via STDIN (ie instead of typing it.)

Anybody know how this is done? I have access to a linux box I can test it on but I don't know the syntax...I heard something about an arrow operator?

Does linux also send the end of file character? I'm trying to figure out how I can tell when I'm done with the input.

My program works fine when I give it the input other ways, I just want to ensure I don't lose 60% of the points due to some stupid I/O error specific to how he's sending in the input.

In case you're curious, the program converts a DFA into a regular expression.

Thanks,
SN



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: November 21, 2003 at 11:26:37 Pacific
Reply:

Okay, I figured out the syntax of how to test:
perl 355.pl<"input.txt"
Is this the only way? I think this is what he's talking about.

Here's the first part of my program - Just puts all the input into $buffer.

#!/usr/bin/perl
$char="a";
while($char ne "")
{
read(STDIN, $char, 1);
$buffer=$buffer.$char;
}
print "argument was:\n";
print $buffer;
print "\nargument ended.\n"

Is this obscenely inefficient? Any better ways to do it?

Thanks again,
-SN


0

Response Number 2
Name: FishMonger
Date: November 21, 2003 at 17:54:09 Pacific
Reply:

There are several ways to do it.

perl 355.pl input.txt

#!/usr/bin/perl

while(<>) { # could be written as while(<STDIN>)
$line = $_;
print $line;
# do something with the line
}

**********

option 2

@file = <>;
foreach (@file) {
# do something
}

**********

option 3

{
local $/;
$file = <>; # entire file is loaded into $file
}

I can probably come up with a couple of other metods.



0

Response Number 3
Name: SN
Date: November 21, 2003 at 19:11:48 Pacific
Reply:

Fish-
Thanks a lot! I like option 3...It works great in the program, and seems more elegant than using a loop. Although I don't understand how it works...what is the $/?

Thanks again,
-SN


0

Response Number 4
Name: FishMonger
Date: November 21, 2003 at 19:51:43 Pacific
Reply:

$/ is the scalar that defines the input record separator (which normally is \n), but that line "undefines" it so you can slurp up the entire file into a single scalar. This is a quick and "dirty" way to read-in the file, but it could make it more difficult to work with the data, depending on what you need to do with it.


0

Response Number 5
Name: FishMonger
Date: November 21, 2003 at 23:34:22 Pacific
Reply:

SN,

I think you and I are just about the only ones on this site using Perl. For more indepth help, you might look at:

experts-exchange


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Linux: Sending a file as STDIN

Run a file as local administrator www.computing.net/answers/programming/run-a-file-as-local-administrator/11442.html

winsock:sending a file www.computing.net/answers/programming/winsocksending-a-file/996.html

Export a file as JPEG format www.computing.net/answers/programming/export-a-file-as-jpeg-format/8347.html