LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-14-2007, 08:08 PM   #1
haydari
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Rep: Reputation: 0
question about input output redirection in c


Hi,

I can redirect the output with a code like this:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main()
{
int i;
FILE *fid;
fid=open("foo.txt",O_WRONLY | O_CREAT);
close(1);
dup(fid);
close(fid);
for (i =0; i<100;i++)
{
printf("i=%d (%d)\n",i,getpid());
}
}

but when it comes to redirecting input, I can't do this.

fid=open("foo.txt",O_WRONLY | O_CREAT);
close(0);
dup(fid);
close(fid);

doesn't just work. Do you have any idea why and how? :=)
 
Old 04-14-2007, 09:21 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
When you say "doesn't work", you mean you can't input from fd 0 after that?

You opened it O_WRONLY|O_CREAT, right? You can't input from something opened thus. Is this what you intended?

Last edited by wjevans_7d1@yahoo.co; 04-14-2007 at 09:23 PM.
 
Old 04-15-2007, 09:51 AM   #3
haydari
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Original Poster
Rep: Reputation: 0
sorry, I did with O_RDONLY and it is ok now.

But I have a new problem.

I can send data to child process with write(fd[1], char_array, strlen(char_array);

I will use it in the child process to redirect the input stream to char_array.

BUT I need to redirect the input to a file not an array.

fid=fid2=open("yarrak.txt",O_RDONLY);
write(fd[1], fid2, 100); 100 is assuming in the file there at most 100 chars. But this doesn't work???

Last edited by haydari; 04-15-2007 at 10:05 AM.
 
Old 04-16-2007, 07:52 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
I'm not sure I understand the problem.

Read into an array. Write from the array to the output file.

Put that in a loop, and exit the loop when there is nothing more to read.

What am I missing?
 
  


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
EOF and input redirection in C on OS X problem vdogvictor Programming 11 05-18-2009 09:21 PM
Implementing Input Redirection amitbern Programming 3 11-06-2006 12:04 PM
LXer: Input / Output Redirection made simple in Linux LXer Syndicated Linux News 0 01-06-2006 10:31 PM
wxPython GUI input/output simple question Hal Programming 0 04-14-2005 03:17 PM
Output & input redirection serotonincy Programming 3 04-12-2004 08:28 AM

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

All times are GMT -5. The time now is 09:19 AM.

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