LinuxQuestions.org
Help answer threads with 0 replies.
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 11-23-2006, 01:40 PM   #1
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Rep: Reputation: 15
DUP2 Help


Hello

I m facing problems using DUP2

my target is to do the following

sort < source > destination

I know how to do sort < source:

fd = open(source, O_RDWR | O_CREAT, mode);
if(dup2(fd, 0) < 0)
close(fd);


I know how to do sort > destination:

fd = open(source, O_RDWR | O_CREAT, mode);
if(dup2(fd, 1) < 0)
close(fd);


I read the man
it s daying dup2(old file, new file)


How to cobine the 2 processes together , it is not working with me :'(
 
Old 11-23-2006, 02:53 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Just a guess:

1. When you "close()" a file descriptor ... the SAME number can be RE-USED for any SUBSEQUENT "open()".

2. So try this:
a) Do your "open()" and your "dup2()" FIRST
b) Do the "close()" AFTERWARDS

'Hope that helps .. PSM
 
Old 11-23-2006, 03:04 PM   #3
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
Do you mean

fd = open(source, O_RDWR | O_CREAT, mode);
if(dup2(fd, 0) < 0)
fd2 = open(destination, O_RDWR | O_CREAT, mode);
if(dup2(fd2, 1) < 0)
close(fd);
close (fd2);


???

I did it still dind't work
 
Old 11-23-2006, 03:17 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
C'mon, studentlb. Give me a BREAK, will you?!?

No - I don't mean:
Code:
  // Stupid code segment
  
  // Open file.  Contents of "source" and "mode" unknown...
  fd = open(source, O_RDWR | O_CREAT, mode);

  // Don't check success of "open()".  Just try to "dup2()" descriptor, regardless
  if(dup2(fd, 0) < 0)
    // If "dup2()' FAILS, then treat it as SUCCESS and try a second "open()"
    fd2 = open(destination, O_RDWR | O_CREAT, mode);
  // Do a second "dup2()"
  if (dup2(fd2, 1) < 0)
    // Only close "fd" if dup2 fails...
    close(fd);
  // but close fd2 all the time
  close (fd2);
Now you OBVIOUSLY know that's not what I meant, don't you?

Please: be a bit more specific about what you're trying to do, and exactly what's going wrong.

And be sure to use "[code]" blocks when you post code segments.

Good luck .. PSM
 
Old 11-23-2006, 03:22 PM   #5
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
ok.

what i need is to do the following

sort < input > output

i know to do each part alone
sort < input
will output the result to the screen

but i need to results to be send to the file called "ouput" and not to the screen

That's why i was trying to create another fd2

did u get what i mean ?

Last edited by studentlb; 11-23-2006 at 03:27 PM.
 
Old 11-23-2006, 04:05 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, again studentlb -

OK, fair enough:

1.
Quote:
what i need is to do the following

sort < input > output

i know to do each part alone
sort < input
will output the result to the screen

but i need to results to be send to the file called "ouput" and not to the screen
2. If you use "<", then you don't need to do anything: the shell handles the redirection for you automatically.

3. However, if you wanted to emulate "<" and ">" yourself, you can do it as follows. You'll notice that it does NOT need "dup2()":
Code:
/*
 * simple test: redirect stdin from first file, redirect stdout to second
 */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/fcntl.h>

int
main (int argc, char *argv[])
{
  int fd1, fd2;
  char buf[1024];
  int i;

  /* Chk cmd-line args */
  if (argc < 3)
  {
    printf ("USAGE: myprog <infile> <outfile>!\n");
    return 1;
  }

  /* This redirects stdin (descriptor 0) from specified file */
  close (0);
  fd1 = open (argv[1], O_RDONLY);
  if (fd1 < 0)
  {
    perror ("Unable to redirect input!");
    return 1;
  }

  /* This redirects stdout (descriptor 1) to specified file */
  close (1);
  fd2 = open (argv[2], O_WRONLY|O_CREAT);
  if (fd2 < 0)
  {
    perror ("Unable to redirect output!");
    return 1;
  }

  /* Now simply copy the file contents */
  while ((i = read(fd1, buf, sizeof (buf))) > 0)
  {
    write (fd2, buf, i);
  }

  /* Close up show and exit */
  close (1);
  close (0);
  return 0;
}
Here's a sample:
Quote:
cc -g -Wall -o x x.c

cp -p x.c tmp1
ls -l tmp?
-rw-r--r-- 1 paulsm users 958 2006-11-23 13:55 tmp1

./x tmp1 tmp2

ls -l tmp?
-rw-r--r-- 1 paulsm users 958 2006-11-23 13:55 tmp1
---------- 1 paulsm users 958 2006-11-23 13:56 tmp2
You'll notice that because I didn't specify a "mode" when I created the file, I didn't get any permissions on the resulting file.

More important, you'll noticed that CLOSING a file descriptor allows me to RE-OPEN the SAME descriptor. That's precisely how the shell happens to implement redirection with the "<" and ">" operators.

'Hope that helps .. PSM
 
Old 11-23-2006, 04:21 PM   #7
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
Thanks PSM, i will work on it
 
  


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



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

All times are GMT -5. The time now is 03:52 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