LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2004, 12:17 PM   #1
scatterbrain
LQ Newbie
 
Registered: Feb 2004
Location: Toronto, Canada
Distribution: Slackware 10.0
Posts: 12

Rep: Reputation: 0
redirecting problems


Hi:

My program reads in from stdin, example:

./myprogram < data

When I try to redirect the output to another file it works fine, example:

./myprogram < data > new.data

But when I try to redirect the output back into the same input file I get an error from my program for some reason, example:

./myprogram < data > data

What I don't get is why my program would care what file I'm redirecting the output to? Because it doesnt seem to like receiving input and directing output to the same file.

Any ideas? Thanks..
 
Old 07-26-2004, 12:27 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
You can't get there from here. The problem is that when the shell sees "> foo", it truncates foo immediately, before running the command line. Your program will see an empty file, and I'm guessing that the error comes from that. Posting what the error is would help though.

example:
Code:
13:24 aluser@alf:/tmp$ echo blah > foo
13:24 aluser@alf:/tmp$ cat < foo > foo
13:24 aluser@alf:/tmp$ cat foo
13:24 aluser@alf:/tmp$
The contents of foo disappear with the cat < foo > foo line.
 
Old 07-26-2004, 12:30 PM   #3
scatterbrain
LQ Newbie
 
Registered: Feb 2004
Location: Toronto, Canada
Distribution: Slackware 10.0
Posts: 12

Original Poster
Rep: Reputation: 0
I see, so is there any way around that besides creating a temp file then renaming it?
 
Old 07-26-2004, 12:42 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
either that or take a filename as an argument, read the whole thing into memory, truncate it, and write it back out.
 
Old 07-26-2004, 12:46 PM   #5
scatterbrain
LQ Newbie
 
Registered: Feb 2004
Location: Toronto, Canada
Distribution: Slackware 10.0
Posts: 12

Original Poster
Rep: Reputation: 0
Renaming a temp would be easier in my case. Thanks for your help! Cheers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting Console MadnessASAP Linux - Software 3 10-19-2005 12:42 AM
Google Redirecting? the_imax General 1 12-31-2004 11:01 AM
redirecting X to windoze synapse Slackware 4 11-30-2004 02:15 AM
Redirecting Half_Elf Linux - Networking 2 09-04-2002 10:51 PM
about port redirecting cmardhekar Linux - Newbie 1 09-13-2001 07:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration